Blazored / LocalStorage

A library to provide access to local storage in Blazor applications
https://blazored.github.io/LocalStorage/
MIT License
1.23k stars 117 forks source link

Unhandled exception rendering component: Please use dedicated worker for working with JavaScript interop. See https://aka.ms/dotnet-JS-interop-threads #230

Open SGStino opened 11 months ago

SGStino commented 11 months ago

Describe the bug When using threads in a blazored application, the local storage functions break, even when called from thread 1.

To Reproduce LocalStorage.SetItemAsStringAsync("key", "value", default);

Expected behavior No exception and saved state.

Logs

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Please use dedicated worker for working with JavaScript interop. See https://aka.ms/dotnet-JS-interop-threads
System.InvalidOperationException: Please use dedicated worker for working with JavaScript interop. See https://aka.ms/dotnet-JS-interop-threads
   at System.Runtime.InteropServices.JavaScript.JSSynchronizationContext.AssertWebWorkerContext()
   at System.Runtime.InteropServices.JavaScript.JSFunctionBinding.BindJSFunctionImpl(String functionName, String moduleName, ReadOnlySpan`1 signatures)
   at System.Runtime.InteropServices.JavaScript.JSFunctionBinding.BindJSFunction(String functionName, String moduleName, ReadOnlySpan`1 signatures)
   at WebAssembly.JSInterop.InternalCalls.InvokeJSJson(String identifier, Int64 targetInstanceId, Int32 resultType, String argsJson, Int64 asyncHandle)
   at Microsoft.JSInterop.WebAssembly.WebAssemblyJSRuntime.BeginInvokeJS(Int64 asyncHandle, String identifier, String argsJson, JSCallResultType resultType, Int64 targetInstanceId)
   at Microsoft.JSInterop.JSRuntime.InvokeAsync[IJSVoidResult](Int64 targetInstanceId, String identifier, CancellationToken cancellationToken, Object[] args)
   at Microsoft.JSInterop.JSRuntime.InvokeAsync[IJSVoidResult](String identifier, CancellationToken cancellationToken, Object[] args)
   at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime jsRuntime, String identifier, CancellationToken cancellationToken, Object[] args)
   at Blazored.LocalStorage.BrowserStorageProvider.SetItemAsync(String key, String data, CancellationToken cancellationToken)
   at Blazored.LocalStorage.LocalStorageService.SetItemAsStringAsync(String key, String data, CancellationToken cancellationToken)

Hosting Model (is this issue happening with a certain hosting model?):

Additional context I suspect it has something to do with the fact that JS interop is required to be invoked from the main thread. But due to the use of ConfigureAwait(false) the thread changes to one of the threadpool.