Blazor WebAssembly 监控线程异常

Blazor WebAssembly Monitor Threading exception

我目前正在开发 .NET Standard 2.1 Blazor WebAssembly 托管应用程序。

我的应用程序结构如下所示:

我在 BlazorApp.Client 项目中将 Serilog 与 Elasticsearch 接收器结合使用。它工作正常,但是当我在 Serilog 上启用 Selflog 并进行调试时,我在浏览器控制台中收到以下错误:

SelfLog.Enable(msg => Debug.WriteLine(msg));

Elasticsearch.Net.UnexpectedElasticsearchClientException:无法在此运行时等待监视器。 ---> System.Threading.SynchronizationLockException:无法在此运行时等待监视器。

at (wrapper managed-to-native) System.Threading.Monitor.Monitor_wait(object,int)

at System.Threading.Monitor.ObjWait (System.Boolean exitContext, System.Int32 millisecondsTimeout, System.Object obj) <0x36c60c8 + 0x00046> in :0

at System.Threading.Monitor.Wait (System.Object obj, System.Int32 millisecondsTimeout, System.Boolean exitContext) <0x36c5de8 + 0x00022> in :0

这似乎是当前 Blazor WASm 版本中的一个问题:https://github.com/dotnet/aspnetcore/issues/22400

Cannot Wait on monitors 有谁知道如何消除 Blazor WebAssembly 客户端中的这个错误?

您不能 'get rid of' 在 Blazor WebAssembly 中出现该错误。 WebAssembly 代码是(暂时)single-threaded 所以执行 System.Threading.Monitor.Wait(something); 将是一个有保证的死锁。

框架正确地向您发出此代码不适合 WebAssembly 的信号。

您必须删除任何需要 Wait() 的代码。