Hi i have an error System.TypeInitializationException: ‘The type initializer for ‘MongoDB.Driver.Core.Misc.DnsClientWrapper’ threw an exception.’ can you help me with this.
MongoDB .NET/C# Driver version : 2.17.1
.NET version : .net 2.0 Standard
Operating system and version : Windows 11 21H2
Full stack trace : System.TypeInitializationException: 'The type initializer for 'MongoDB.Driver.Core.Misc.DnsClientWrapper' threw an exception.' and System.AggregateException: 'Error resolving name servers (Object reference not set to an instance of an object.) (Could not find file "/etc/resolv.conf")'
Hey, can you provide your scenario and a small repro? Does it work without xamarin like in a simple console app?
No it’s a xamarin app for a mobile device like a phone or tablet here’s a picture of the code where I apply the Mongo DB commands!
var settings = MongoClientSettings.FromConnectionString("<connectionString>");
var client = new MongoClient(settings);
var database = client.GetDatabase("<myDatabase>");
var collection = database.GetCollection<BsonDocument>("<My collection>");
var document = new BsonDocument
{
{ "name", "MongoDB" },
{ "type", "Database" },
{ "count", 1 },
{ "info", new BsonDocument
{
{ "x", 203 },
{ "y", 102 }
}}
};
await collection.InsertOneAsync(document);
This error happens in 3rd party dependency that we reference in the driver with this simple code: new LookupClient()
. I think you will see the same error when you create a LookupClient directly without driver at all. Is so, I think the best solution here is if you create an issue here
NOTE: for xamarin, you can try using realm driver that most-likely doesn’t have this issue