MichaCo / DnsClient.NET

DnsClient.NET is a simple yet very powerful and high performant open source library for the .NET Framework to do DNS lookups
https://dnsclient.michaco.net
Apache License 2.0
762 stars 136 forks source link

Error System.TypeInitializationException: ‘The type initializer for ‘MongoDB.Driver.Core.Misc.DnsClientWrapper’ threw an exception.’ #171

Closed Cinfob09 closed 1 year ago

Cinfob09 commented 1 year ago

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")'

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);

I talk to an MongoDB employee and he respond me to create an issue here !

MichaCo commented 1 year ago

Is that really a Windows machine this is running on. The code here tries to find DNS config on non windows machines. This shouldn't run if its windows (or that check does not return the right OS -.-)

I'll take a look later but maybe you can give me more details about your environment that code is running in?

Thanks

Cinfob09 commented 1 year ago

Hello, I am a beginner and I am on windows 11 and my IDE is Visual Studio do you know what I have to do to make it find the DNS configuration on windows machines. Thanks a lot for your help! Cinfob09

MichaCo commented 1 year ago

Hey @Cinfob09 , no problem ;) The DNS configuration would come from your network adapter. Usually that just works. There might be problems if you use a private VPN for example, those sometimes block any DNS traffic which isn't controlled by the VPN. Also, it matters where you run your code. Is it just a console app your run from VS? Or is it running in a container or WSL? In that case it might be a different story

That being said, under Windows, that code which causes the error you are getting should not even run. So, it looks like a bug, I'll take a look

Cinfob09 commented 1 year ago

Hi, I don't use WSL and it's not a console app it's an app that will run on a mobile device like a tablet or android and iOS phone. I will check my network card.

MichaCo commented 1 year ago

Getting the DNS server on a mobile device works completely differently and isn't supported by this library. Are you running the mongodb client in your mobile app / via an emulator in Visual Studio?

Cinfob09 commented 1 year ago

Yes, but I changed my network card settings and now no error message appears but it can't add my json document to the database. I checked my connection string and it works in mongo db compass and it has the authorization to import the document.

MichaCo commented 1 year ago

well, good luck with googling for help for that ;) /closing the issue