Issue
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!
Solution
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:
- 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.
- 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.
Answered By - Carl de Billy
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)