MAUI Blazor App on Android Device how to connect to MongoDB Atlas (ConnectionString works on Windows PC but not on Android Device)?

Hi,
I coded a MAUI Blazor App which connects to MongoDB Atlas and loads some Data in the App. Everything worlks fine if I run the App on a Windows PC. But if I compile/run the same code on an Android device I get this error in the development tools:

blazor.webview.js:1 List of configured name servers must not be empty. (Parameter ‘servers’)
at DnsClient.LookupClient.QueryInternal(DnsQuestion question, DnsQuerySettings queryOptions, IReadOnlyCollection`1 servers)
at DnsClient.LookupClient.Query(DnsQuestion question)
at DnsClient.LookupClient.Query(String query, QueryType queryType, QueryClass queryClass)
at MongoDB.Driver.Core.Misc.DnsClientWrapper.ResolveTxtRecords(String domainName, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Configuration.ConnectionString.Resolve(Boolean resolveHosts, CancellationToken cancellationToken)
at MongoDB.Driver.MongoUrl.Resolve(Boolean resolveHosts, CancellationToken cancellationToken)
at MongoDB.Driver.MongoClientSettings.FromUrl(MongoUrl url)
at MongoDB.Driver.MongoClientSettings.FromConnectionString(String connectionString)
at MongoDB.Driver.MongoClient…ctor(String connectionString)
at MBz_MauiAndMongoDb.Pages.Index.OnInitializedAsync() in C:\Users\Are\source\repos\MBz_MauiAndMongoDb\MBz_MauiAndMongoDb\Pages\Index.razor:line 23
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

The error occures at is moment:

var client = new MongoClient(MongoDbConnectionString);
var db = client.GetDatabase(MongoDbName);

The ConnectionString works perfect if run on a Windows maschine. The ConnectionString looks like this:

“mongodb+srv://AtlasUser: myUserName+myCluster/test?retryWrites=true&w=majority&ssl=true”

I use the MongoDB.Bson and the C# MongoDB.Driver.

In the Documentations I only find the connectionString Syntax I use. But that seems to NOT work with MAUI App on a Android-Device! If I just use “mongodb://AtlasUser:…” without the ‘srv’ I get a timeout error. Any Ideas?

Thx for your Help!

2 Likes

Hi, @Henning,

Welcome back to the MongoDB Community Forums. I see that you’re having problems running a MAUI Blazor app using the MongoDB .NET/C# Driver. The error you’ve encountered is coming from one of our third-party dependencies, DnsClient.NET, which we use for SRV and TXT DNS lookups. The error indicates that autodiscovery of nameservers wasn’t successful. You can read more about it in DnsClient.NET issue #143.

While in theory you could manually configure DNS nameservers when instantiating the LookupClient instance, our driver does not expose the ability to supply arguments to the LookupClient constructor.

Two options:

  1. Figure out why DnsClient.NET cannot autodiscover the configured DNS nameservers.
  2. Use the standard connection string format (e.g. mongodb://) instead of the DNS seed list connection format (e.g. mongodb+srv://).

The latter option works because we only use DnsClient.NET for SRV and TXT lookups. A, AAAA, CNAME, and other DNS records are performed automatically by the operating system. Note that simply removing +srv from the connection string is not sufficient. The hostnames are different. If you’re using MongoDB Atlas, go to your cluster page, select Connect, Connect your application, C# / .NET 2.4 or later, and you should see a connection string that looks like:

mongodb://<username>:<password>@cluster0-shard-00-00.CLUSTER_NAME.mongodb.net:27017,cluster0-shard-00-01.CLUSTER_NAME.mongodb.net:27017,cluster0-shard-00-02.CLUSTER_NAME.mongodb.net:27017/?ssl=true&replicaSet=REPLSET_NAME&authSource=admin&retryWrites=true&w=majority

Please let us know if you have any additional questions.

Sincerely,
James

1 Like

Thanky you very much… yes this solved the problem:

Note that simply removing +srv from the connection string is not sufficient. The hostnames are different. If you’re using MongoDB Atlas, go to your cluster page, select Connect , Connect your application , C# / .NET 2.4 or later , and you should see a connection string that looks like:

mongodb://<username>:<password>@cluster0-shard-00-00.CLUSTER_NAME.mongodb.net:27017,cluster0-shard-00-01.CLUSTER_NAME.mongodb.net:27017,cluster0-shard-00-02.CLUSTER_NAME.mongodb.net:27017/?ssl=true&replicaSet=REPLSET_NAME&authSource=admin&retryWrites=true&w=majority
1 Like

Hello, I recently ran into this same issue and am worried about using the MongoDB without the srv for a production environment. Does anyone know if there has been any sort of development on this issue? This seems like it could turn into a serious issue for adoption of MongoDB on MAUI Blazor applications.

Hi, @Chas_Phyle,

Thank you for your additional question. MongoDB supports two types of connection strings, standard (mongodb://) and DNS seed list (mongodb+srv://). Certain features - like srvMaxHosts and dynamic mongos discovery - are only possible with the latter. Otherwise the two connection string formats are equivalent.

For the .NET/C# Driver in particular, we use a third-party library, DnsClient.NET, for looking up SRV and TXT DNS records because the BCL does not support this out of the box. By default, DnsClient.NET uses the default DNS servers configured at the operating system level. Depending on your execution environment (e.g. mobile apps on some platforms), the default DNS servers configured by the operating system are not accessible due to the security sandbox. This results in DnsClient.NET failing to initialize correctly. We resolved this recently in 2.19.1 (see CSHARP-4436) by avoiding initialization of DnsClient.NET unless it is required (e.g. mongodb+srv:// is used).

In summary mongodb:// and mongodb+srv:// are two different connection string formats and both are supported. If your execution environment only supports mongodb:// connection strings, you can still connect successfully to your MongoDB Atlas and self-hosted clusters.

Sincerely,
James