Hi, I am finding an error whenever I try to instantiate a MongoClient or a MongoClientSettings object (from MongoDB.Driver) when passing the connection string to their constructor.
The whole error message that I can get a hold off says:
{System.TypeInitializationException: The type initializer for ‘MongoDB.Driver.Core.Misc.DnsClientWrapper’ threw an exception. —> System.AggregateException: Error resolving name servers (Object reference not set to an instance of an object.) (Could not find file “/etc/resolv.conf”) —> System.NullReferenceException: Object reference not set to an instance of an object. at DnsClient.NameServer.QueryNetworkInterfaces () [0x0004c] in <519bb9af32234e5dba6bd0b076a88151>:0 at DnsClient.NameServer.ResolveNameServers (System.Boolean skipIPv6SiteLocal, System.Boolean fallbackToGooglePublicDns) [0x0005e] in <519bb9af32234e5dba6bd0b076a88151>:0 — End of inner exception stack trace — at DnsClient.NameServer.ResolveNameServers (System.Boolean skipIPv6SiteLocal, System.Boolean fallbackToGooglePublicDns) [0x00192] in <519bb9af32234e5dba6bd0b076a88151>:0 at DnsClient.LookupClient…ctor (DnsClient.LookupClientOptions options, DnsClient.DnsMessageHandler udpHandler, DnsClient.DnsMessageHandler tcpHandler) [0x000bc] in <519bb9af32234e5dba6bd0b076a88151>:0 at DnsClient.LookupClient…ctor (DnsClient.LookupClientOptions options) [0x00000] in <519bb9af32234e5dba6bd0b076a88151>:0 at DnsClient.LookupClient…ctor () [0x00006] in <519bb9af32234e5dba6bd0b076a88151>:0 at MongoDB.Driver.Core.Misc.DnsClientWrapper…ctor () [0x00006] in :0 at MongoDB.Driver.Core.Misc.DnsClientWrapper…cctor () [0x00000] in :0 — End of inner exception stack trace — at MongoDB.Driver.Core.Configuration.ConnectionString…ctor (System.String connectionString) [0x00000] in :0 at MongoDB.Driver.MongoUrlBuilder.Parse (System.String url) [0x00000] in <27273b0202ea4c34867b683ed7b21818>:0 at MongoDB.Driver.MongoUrlBuilder…ctor (System.String url) [0x00006] in <27273b0202ea4c34867b683ed7b21818>:0 at MongoDB.Driver.MongoUrl…ctor (System.String url) [0x00000] in <27273b0202ea4c34867b683ed7b21818>:0 at MongoDB.Driver.MongoClientSettings.FromConnectionString (System.String connectionString) [0x00000] in <27273b0202ea4c34867b683ed7b21818>:0 at --REDACTED FILENAME–
The lines of code that cause this error are:
string connString = "mongodb+srv://" + user + ":" + secret + "@" + server + "/"+ project +"?retryWrites=true&w=majority";
var settings = MongoClientSettings.FromConnectionString(connString);
settings.ServerApi = new ServerApi(ServerApiVersion.V1);
var mongoClient = new MongoClient(settings);
It will break at MongoClientSettings.FromConnectionString(connString);
Or this will also break:
string connString = "mongodb+srv://" + user + ":" + secret + "@" + server + "/"+ project +"?retryWrites=true&w=majority";
var mongoClient = new MongoClient(connString);
This issue is only happening when I run this code in a Xamarin project. I have the exact same code in a .NET 6 project and everything works fine there. The Xamarin project on the other hand targets .NET Standard 2.0 (also tried 2.1 with same issue) and I’ve been debugging it in an Android 12 device. The driver versions I’ve tested are 2.4.4 and 2.18.0. IDE is Visual Studio Version 17.2.0
I appreciate any help I could get here. Thank you.