[CSHARP-4436] .NET/C# driver fails to create new mongo client in xamarin Created: 01/Dec/22  Updated: 28/Oct/23  Resolved: 02/Mar/23

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 2.19.1

Type: Bug Priority: Major - P3
Reporter: Santiago Suarez Assignee: Dmitry Lukyanov (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Issue split
split to CSHARP-4556 Consider custom LookupClient configur... Backlog
Case:
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

Error is thrown whenever I try to instantiate a MongoClient or a MongoClientSettings object (from MongoDB.Driver) when passing the connection string to their constructor, when running in Xamarin. 

{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–

 

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

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

How to Reproduce

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

 

 

Additional Background

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



 Comments   
Comment by Githook User [ 24/Mar/23 ]

Author:

{'name': 'Dmitry Lukyanov', 'email': 'dmitry.lukyanov@mongodb.com', 'username': 'DmitryLukyanov'}

Message: CSHARP-4436: .NET/C# driver fails to create new mongo client in xamarin. (#1036)
Branch: v2.19.x
https://github.com/mongodb/mongo-csharp-driver/commit/cbc46103c1082dbe7a605631a4974c533c0f01ea

Comment by Dmitry Lukyanov (Inactive) [ 02/Mar/23 ]

The scope of this ticket was limited to making DnsClientWrapper lazy that will allow avoiding this issue for cases where srv resolving is not required. Please follow to CSHARP-4556 for further improvements here

Comment by Githook User [ 02/Mar/23 ]

Author:

{'name': 'Dmitry Lukyanov', 'email': 'dmitry.lukyanov@mongodb.com', 'username': 'DmitryLukyanov'}

Message: CSHARP-4436: .NET/C# driver fails to create new mongo client in xamarin. (#1036)
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/e896c22321aba02023aa38f84b80524bdeab99c4

Comment by James Kovacs [ 02/Dec/22 ]

The .NET/C# Driver uses DnsClient.NET, a third-party DNS library, for resolving SRV and TXT records. Unfortunately it appears that increased security restrictions around DNS introduced in Android Oreo prevent DnsClient.NET from working correctly. See issue #17 in DnsClient.NET's issue tracker. Given that the issue is closed, it doesn't appear that a fix is forthcoming.

You can work around this issue by using the standard connection string format (AKA mongodb://) rather than the DNS seedlist format (AKA mongodb+srv://). A and CNAME record lookups use .NET's built-in capabilities and don't require any third-party libraries. Unfortunately these built-in capabilities do not include SRV and TXT record lookups, which is why we depend on DnsClient.NET for these record types.

Closing this ticket as this is a limitation of a third-party library and we are not aware of another third-party library that can work around this issue.

Generated at Wed Feb 07 21:48:13 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.