Skip to content

There is an error "An exception occurred executing JS interop: DeserializeUnableToConvertValue" in browser console when running the published WebWorker with Blazor WASM app #66346

@jinzhao1127

Description

@jinzhao1127

REGRESSION INFO: Works well on 11.0 Preview 3

INSTALL STEPS

  1. Clean Win11 x64 23h2 ENU
  2. Install the .NET 11.0 preview 4
  3. Apply feed

Platform

  • Windows
  • macOS
  • Linux

Repro Steps

  1. Open a normal Command Prompt
  2. Create a Blazor WebAssembly Standalone App with a WebWorker library
dotnet new blazorwasm -n MyApp
dotnet new blazorwebworker -n MyWebWorker
  1. Add a project reference from the Blazor app to the WebWorker library:
cd MyApp
dotnet add reference ../MyWebWorker/MyWebWorker.csproj
  1. Open .csproj of Blazor WebAssembly app and add the following inside the existing :
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>

  2. 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);
}
  1. 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", []);
    }
}
  1. Install the dotnet-serve tool
    dotnet tool install --global dotnet-serve

  2. Publish the application to folder:
    dotnet publish -c Release

  3. Go to the publish output directory and run dotnet-serve -S:

cd bin\Release\net11.0\publish\wwwroot
dotnet-serve -S
  1. Open a browser and navigate to the URL

Note:

  1. This issue does not repro when dotnet run
  2. I notices that the webworker template change to blazorwebworker

Actual Result
An unhandled error has occurred.

Image

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)

Metadata

Metadata

Assignees

Labels

area-blazorIncludes: Blazor, Razor ComponentsbugThis issue describes a behavior which is not expected - a bug.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions