REGRESSION INFO: Works well on 11.0 Preview 3
INSTALL STEPS
- Clean Win11 x64 23h2 ENU
- Install the .NET 11.0 preview 4
- Apply feed
Platform
Repro Steps
- Open a normal Command Prompt
- Create a Blazor WebAssembly Standalone App with a WebWorker library
dotnet new blazorwasm -n MyApp
dotnet new blazorwebworker -n MyWebWorker
- Add a project reference from the Blazor app to the WebWorker library:
cd MyApp
dotnet add reference ../MyWebWorker/MyWebWorker.csproj
-
Open .csproj of Blazor WebAssembly app and add the following inside the existing :
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
-
Create a Myworker.cs file in the Blazor WebAssembly app, add following code snip.
using System.Runtime.InteropServices.JavaScript;
using System.Runtime.Versioning;
using System.Text.Json;
namespace MyApp
{
[SupportedOSPlatform("browser")]
public static partial class MyWorker
{
[JSExport]
public static string Greet(string name) => $"Hello, {name}!";
[JSExport]
public static string GetUsers()
{
var users = new List<User> { new("Alice", 30), new("Bob", 25) };
return JsonSerializer.Serialize(users); // Serialize before returning
}
}
public record User(string Name, int Age);
}
- In the Home.razor file, add the following code.
@inject IJSRuntime JSRuntime
@using MyWebWorker
@code {
protected override async Task OnInitializedAsync()
{
await using var worker = await WebWorkerClient.CreateAsync(JSRuntime);
Console.WriteLine("Worker created?");
var greeting = await worker.InvokeAsync<string>("MyApp.MyWorker.Greet", ["World"]);
var users = await worker.InvokeAsync<List<User>>("MyApp.MyWorker.GetUsers", []);
}
}
-
Install the dotnet-serve tool
dotnet tool install --global dotnet-serve
-
Publish the application to folder:
dotnet publish -c Release
-
Go to the publish output directory and run dotnet-serve -S:
cd bin\Release\net11.0\publish\wwwroot
dotnet-serve -S
- Open a browser and navigate to the URL
Note:
- This issue does not repro when dotnet run
- I notices that the
webworker template change to blazorwebworker
Actual Result
An unhandled error has occurred.
Error Log
blazor.webassembly.tmoplnooj7.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: An exception occurred executing JS interop: DeserializeUnableToConvertValue, System.Collections.Generic.List`1[MyApp.User] Path: $ | LineNumber: 0 | BytePositionInLine: 65.. See InnerException for more details.
Microsoft.JSInterop.JSException: An exception occurred executing JS interop: DeserializeUnableToConvertValue, System.Collections.Generic.List`1[MyApp.User] Path: $ | LineNumber: 0 | BytePositionInLine: 65.. See InnerException for more details.
---> System.Text.Json.JsonException: DeserializeUnableToConvertValue, System.Collections.Generic.List`1[MyApp.User] Path: $ | LineNumber: 0 | BytePositionInLine: 65.
at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type)
at System.Text.Json.Serialization.JsonCollectionConverter`2[[System.Collections.Generic.List`1[[MyApp.User, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[MyApp.User, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnTryRead(Utf8JsonReader&, Type, JsonSerializerOptions, ReadStack& , List`1& )
at System.Text.Json.Serialization.JsonConverter`1[[System.Collections.Generic.List`1[[MyApp.User, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TryRead(Utf8JsonReader&, Type, JsonSerializerOptions, ReadStack& , List`1& , Boolean& )
at System.Text.Json.Serialization.JsonConverter`1[[System.Collections.Generic.List`1[[MyApp.User, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ReadCore(Utf8JsonReader&, List`1& , JsonSerializerOptions, ReadStack&)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1[[System.Collections.Generic.List`1[[MyApp.User, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Deserialize(Utf8JsonReader&, ReadStack&)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1[[System.Collections.Generic.List`1[[MyApp.User, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].DeserializeAsObject(Utf8JsonReader&, ReadStack&)
at System.Text.Json.JsonSerializer.ReadAsObject(Utf8JsonReader&, JsonTypeInfo)
at System.Text.Json.JsonSerializer.Deserialize(Utf8JsonReader&, Type , JsonSerializerOptions )
at Microsoft.JSInterop.JSRuntime.EndInvokeJS(Int64, Boolean, Utf8JsonReader&)
Exception_EndOfInnerExceptionStack
at MyWebWorker.WebWorkerClient.<InvokeAsync>d__5`1[[System.Collections.Generic.List`1[[MyApp.User, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at MyApp.Pages.Home.OnInitializedAsync()
at MyApp.Pages.Home.OnInitializedAsync()
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task, ComponentState)
REGRESSION INFO: Works well on 11.0 Preview 3
INSTALL STEPS
Platform
Repro Steps
Open .csproj of Blazor WebAssembly app and add the following inside the existing :
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>Create a Myworker.cs file in the Blazor WebAssembly app, add following code snip.
Install the dotnet-serve tool
dotnet tool install --global dotnet-servePublish the application to folder:
dotnet publish -c ReleaseGo to the publish output directory and run dotnet-serve -S:
Note:
webworkertemplate change toblazorwebworkerActual Result
An unhandled error has occurred.
Error Log