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!

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