Search code examples
c#multithreadingblazorwebassembly.net-standard-2.1

Blazor WebAssembly Monitor Threading exception


I'm currently working on a .NET Standard 2.1 Blazor WebAssembly hosted application.

My application structure looks like this:

  • BlazorApp.Client (error occures here)
  • BlazorApp.Server

I use Serilog with the Elasticsearch sink in my BlazorApp.Client project. It works fine, but when I enable Selflog on Serilog and debug, I get the following error in my browser console:

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

Elasticsearch.Net.UnexpectedElasticsearchClientException: Cannot wait on monitors on this runtime. ---> System.Threading.SynchronizationLockException: Cannot wait on monitors on this runtime.

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

It seems to be an issue in the current Blazor WASm release: https://github.com/dotnet/aspnetcore/issues/22400

Cannot Wait on monitors Does anyone know, how to possibly get rid of this error in Blazor WebAssembly Client?


Solution

  • You cannot 'get rid of' that error in Blazor WebAssembly. WebAssembly code is (for the time being) single-threaded so executing System.Threading.Monitor.Wait(something); would be a guaranteed deadlock.

    The framework correctly signals to you that this code is not suitable for WebAssembly.

    You will have to get rid of of any code that needs to Wait() (that usually appears as lock(x) { ... } ).