系统.在.net 5上使用mongodb驱动程序时出现了PlatformNotSupportedEx



我有一个blazor wasm app,我正试图连接到mongodb。一旦我做一个FindAsync或连接到数据库的东西,我得到一个异常,这是不支持在这个平台上。我谷歌这个,发现了一些与连接字符串有关的问题,但在我的集成测试中,相同的连接字符串工作正常。MongoClient被注册为单例,我在任何地方使用async await,因为wasm似乎不支持.Result或任何阻塞调用。我在mac上和rider一起开发。我尝试了多种方法连接到数据库,但我总是得到这个神秘的错误。

这是堆栈跟踪:

A timeout occurred after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 }, OperationsCountServerSelector }. Client view of cluster state is { ClusterId : "1", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "157.90.1.251:3513" }", EndPoint: "157.90.1.251:3513", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.
blazor.webassembly.js:1 ---> System.PlatformNotSupportedException: System.Net.Sockets is not supported on this platform.
blazor.webassembly.js:1   at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
blazor.webassembly.js:1   at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateSocket(EndPoint endPoint)
blazor.webassembly.js:1   at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
blazor.webassembly.js:1   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
blazor.webassembly.js:1   --- End of inner exception stack trace ---
blazor.webassembly.js:1   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
blazor.webassembly.js:1   at MongoDB.Driver.Core.Servers.ServerMonitor.InitializeConnectionAsync(CancellationToken cancellationToken)
blazor.webassembly.js:1   at MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken cancellationToken)", LastHeartbeatTimestamp: "2021-08-18T09:47:41.3960000Z", LastUpdateTimestamp: "2021-08-18T09:47:41.3960000Z" }] }.
blazor.webassembly.js:1   at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
blazor.webassembly.js:1   at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedHelper.HandleCompletedTask(Task completedTask)
blazor.webassembly.js:1   at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedAsync(IServerSelector selector, ClusterDescription description, Task descriptionChangedTask, TimeSpan timeout, CancellationToken cancellationToken)
blazor.webassembly.js:1   at MongoDB.Driver.Core.Clusters.Cluster.SelectServerAsync(IServerSelector selector, CancellationToken cancellationToken)
blazor.webassembly.js:1   at MongoDB.Driver.MongoClient.AreSessionsSupportedAfterServerSelectionAsync(CancellationToken cancellationToken)
blazor.webassembly.js:1   at MongoDB.Driver.MongoClient.AreSessionsSupportedAsync(CancellationToken cancellationToken)
blazor.webassembly.js:1   at MongoDB.Driver.MongoClient.StartImplicitSessionAsync(CancellationToken cancellationToken)
blazor.webassembly.js:1   at MongoDB.Driver.MongoCollectionImpl`1.<UsingImplicitSessionAsync>d__106`1[[FadingFlameLeague.UserAccounts.UserAccount, FadingFlameLeague, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[FadingFlameLeague.UserAccounts.UserAccount, FadingFlameLeague, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
blazor.webassembly.js:1   at FadingFlameLeague.Repositories.MongoDbRepositoryBase.<Upsert>d__9`1[[FadingFlameLeague.UserAccounts.UserAccount, FadingFlameLeague, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext() in /Users/simonheiss/IdeaProjects/FadingFlameLeague/FadingFlameLeague/Repositories/MongoDbRepositoryBase.cs:line 63
blazor.webassembly.js:1   at FadingFlameLeague.Pages.Register.OnValidSubmit() in /Users/simonheiss/IdeaProjects/FadingFlameLeague/FadingFlameLeague/Pages/Register.razor:line 55

这就是我如何生成MongoClient

services.AddSingleton(_ =>
{
var mongoConnectionString = "mongodb://157.90.1.251:3513";
return new MongoClient(mongoConnectionString);
});

这样会抛出异常:

private async Task OnValidSubmit()
{
var mongoDatabase = client.GetDatabase("test");
var mongoCollection = mongoDatabase.GetCollection<UserAccount>(typeof(UserAccount).Name);
await mongoCollection.InsertOneAsync(new UserAccount());
}

这真的很令人沮丧,它已经花了我几个小时,我似乎没有发现一个问题。有几篇文章提到wasm和。net标准不能很好地协同工作,而mongoclient是。net标准2.1。但我无法想象这是一个问题,我的意思是我肯定不是第一个同时测试wasm和mongo的人^^

您看到的错误是您试图在wasm运行时打开套接字:

blazor.webassembly.js:1 ---> System.PlatformNotSupportedException: System.Net.Sockets is not supported on this platform.
blazor.webassembly.js:1   at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)

你不能在web浏览器中打开套接字,所以你得到PlatformNotSupportedException

同样,我不相信你会实现从web浏览器直接与MongoDB数据库工作的能力,除非这样的交互将通过HTTP或websocket完成。我希望你需要一个特殊的库。

从安全的角度来看,我不确定是否有一个web客户端直接连接到数据库是一个好主意。

相关内容

  • 没有找到相关文章

最新更新