Home > front end >  MAUI Blazor App can't connect to MongoDB Atlas via ConnectionString, why?
MAUI Blazor App can't connect to MongoDB Atlas via ConnectionString, why?

Time:09-02

I coded a MAUI Blazor App which connects to MongoDB Atlas and loads some Data in the App. Everything works 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\XXX\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. Or is it a problem with permissions under Android, I didn't change the original MAUI Blazor template stuff in that regard. Any Ideas?

Thx for your Help!

CodePudding user response:

It's simple: on WebAssembly you cannot do something impossible to do using JavaScript in a browser.

The mongodb:// protocol is a TCP based protocol and cannot be connected from any kind of code running in a browser. This limitation is not specific to Blazor.. any other WebAssembly like Uno Platform will have the exact same limitations.

You have 2 options to circumvent this:

  1. You connect to a server part of your application using http/https (it could be REST API, WebSockets, SignalR or whatever else running on http) and, from there, you connect to the MongoDB server.
  2. You connect directly to the MongoDB server using a REST interface -- but I'm pretty sure the MongoDB.Driver won't be able to use them and you'll have to build your queries by yourself.

Disclamer: I'm a senior architect at Uno Platform working on the WebAssembly platform.

CodePudding user response:

This answere from the MongoDB Forum solved my problem:

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:

Figure out why DnsClient.NET cannot autodiscover the configured DNS nameservers. 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
  • Related