Random InvalidOperationException in MudBlazor-App
When using BlazorWebView with MudBlazor I randomly get the following exception while changing the value of a MudTimePicker. The exception usually occurs after 5-10 edits.
Exception thrown: 'System.InvalidOperationException' in Microsoft.AspNetCore.Components.WebView.dll: 'Received unexpected acknowledgement for render batch 263 (next batch should be 262)'
at Microsoft.AspNetCore.Components.WebView.Services.WebViewRenderer.NotifyRenderCompleted(Int64 batchId)
The error does not occur when starting the same UI with MAUI Android Blazor Hybrid or Blazor WebApp. Unfortunately I'm not able to reproduce the error in a blank MudBlazor/WebView project and I can't share the project where the error occurs. Do you maybe have an idea what could cause this issue? I have no clue where I should start looking.
Do you maybe have an idea what could cause this issue?
I'm sorry, but I don't. The exception appears to be originating in Microsoft's WebView framework: https://github.com/dotnet/aspnetcore/blob/48fa7802f4c5fbe4b85a92e0322e41e1614e7b5c/src/Components/WebView/WebView/src/Services/WebViewRenderer.cs#L73
But the nature of the error seems to indicate cause being elsewhere.
Which version of the .NET runtime are you using? Can the error be reproduced in .NET 8, 9, and 10?
Thanks for your reply. I'm currently using .net 9 - I will give .net 10 a try once it's out.
Thanks for your reply. I'm currently using .net 9 - I will give .net 10 a try once it's out.
Hi @markusroessler
I think the issue is a typical synchronization problem. MudBlazor components (like the TimePicker) fire a lot of JS-side events, and if the BlazorWebView is not running on the main thread, these render batches can get mixed up, which causes the "Received unexpected acknowledgement for render batch" error.
- First, try starting your application like this:
application.RunWithSynchronizationContext([]);
This ensures that all render and JS interop calls run on a single thread in order.
- If the issue still occurs, try this NuGet package: WebKit.BlazorWebView.GirCore 10.0.0-rc.1 This version is newer than the one in JinShil’s repo and already includes a custom dispatcher that prevents parallel calls from interfering with each other.
If you prefer not to use NuGet, you can copy the source directly into your project: https://github.com/czirok/apps/tree/main/src/WebKit.BlazorWebView.GirCore/src
This version stabilizes the render-ack sequence on GTK/GLib and prevents message mix-ups when multiple threads are active. And of course, make sure the application is started with
application.RunWithSynchronizationContext([]);
Hi @czirok, thank you for the detailed answer.
The exception still occurred with application.RunWithSynchronizationContext([]);
But switching to WebKit.BlazorWebView.GirCore 10.0.0-rc.1 seems to help. I'm not able to reproduce the error anymore.
Thank you!