The following issue was found while connecting to MongoDB using a C# application
Could not load file or assembly ‘System.Runtime.InteropServices.RuntimeInformation, Version=22.214.171.124, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or one of its dependencies. The system cannot find the file specified.
I had installed the Mongo client dlls directly from NuGet. Compilation was successful. however, the error was thrown at the following line:
mongoClient = new MongoClient("mongodb://localhost:27017");
I even tried the default constructor since, by default, it will look at the localhost address only. But it also resulted in same error!
mongoClient = new MongoClient();
The solution was really easy. If you read the error message closely, which I didn’t, initially, 🙄 you’ll see that the error clearly states that my solution was missing the following package,
Just like the Mongo client dlls, this can also be added via NuGet. Just go to Manage NuGet Packages and search for, System.Runtime.InteropServices.RuntimeInformation. Select the following package and install.
As soon as I added this package to my solution, I was then able to successfully connect to MongoDB.