Blazor WebAssembly Monitor线程异常

问题描述

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

我的应用程序结构如下:

  • BlazorApp.Client(此处发生错误
  • BlazorApp.Server

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

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

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

在(包装器托管到本机)System.Threading.Monitor.Monitor_wait(object,int)

,位于System.Threading.Monitor.ObjWait(System.Boolean exitContext,system.int32毫秒超时,System.Object obj) in:0

,位于System.Threading.Monitor.Wait(System.Object obj,system.int32毫秒超时,System.Boolean exitContext),位于:0中的

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

Cannot Wait on monitors有谁知道,如何摆脱Blazor WebAssembly Client中的此错误

解决方法

您无法在Blazor WebAssembly中“摆脱”该错误。 WebAssembly代码(目前)是单线程的,因此执行System.Threading.Monitor.Wait(something);将确保有死锁。

该框架正确地向您表明此代码不适合WebAssembly。

您将必须删除所有需要Wait()的代码。