3

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?

2
  • 1
    I think the current MONO runtime on WASM only supports a single thread. Commented Aug 13, 2020 at 15:29
  • 1
    For SO-users struggling with the same error message, you can consult stackoverflow.com/questions/78684132/…, also answered by Henk Holterman. If that question/answer helps you, please give an upvote to Holterman on this question below. Commented Jun 28, 2024 at 20:16

2 Answers 2

6

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(),
and that usually appears as lock(x) { ... } .

Sign up to request clarification or add additional context in comments.

Comments

0

Adding to Henk's answer:

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) { ... } ).

There are places you can invoke asynchronous methods. Those are your components' Async methods. The most popular of them is OnAfterRenderAsync

Example:

protected override async Task OnAfterRenderAsync(bool firstRender)
{
    await JsRuntime.InvokeVoidAsync("console.log", "This message will show up in the console");
} 

2 Comments

Yes, but async is not the same as threading. Javascript demands that all I/O is async. But do not use .Wait() or .Result on a Task, always await.
You're absolutely right. I never worked with 'Elasticsearch' or 'Serilog', but I experienced the same error message while trying to await a sync method. So I thought to add this answer for future readers that search on the error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.