Skip to content

Cannot call synchronous C# methods for Blazor Web App and WasmEnableThreads #61

@nealles

Description

@nealles
  1. Create Blazor Web App wth InteractiveWebAssembly rendering
  2. Added to Program.cs for the Client:
builder.Services.AddBlazorJSRuntime();
await builder.Build().BlazorJSRunAsync();
  1. Modify Counter.razor like this:
@page "/counter"
@using SpawnDev.BlazorJS
@using SpawnDev.BlazorJS.JSObjects

@rendermode @(new InteractiveWebAssemblyRenderMode(false))

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
<p>@message</p>

@code {
    private string message = "Not clicked yet";
    private int currentCount = 0;

    private string status = "";

    [Inject]
    BlazorJSRuntime JS { get; init; }

    private void IncrementCount()
    {
        var numbers = new object[] { 5, 15, 25 };
        var max = JS.CallApply<int>("Math.max", numbers);
        Console.WriteLine($"Max: {max}");
        currentCount++;
        message = $"Clicked! Thread: {Environment.CurrentManagedThreadId}";
        Console.WriteLine($"Button clicked on thread: {Environment.CurrentManagedThreadId}");
    }
}

  1. Run it and try to change a page to Counter ASAP and click a button "Click me".
  2. You'll see an error in the console:
Uncaught Error: Cannot call synchronous C# methods.
    at Dn (managed-exports.ts:308:23)
    at Object.<anonymous> (invoke-cs.ts:327:17)
    at Vr.St._internal.updateRootComponents (blazor.web.b9228eflpl.js:1:175568)
    at blazor.web.b9228eflpl.js:1:194816
    at Pi.updateWebAssemblyRootComponents (blazor.web.b9228eflpl.js:1:195013)
    at Pi.refreshRootComponents (blazor.web.b9228eflpl.js:1:194498)
    at blazor.web.b9228eflpl.js:1:192714
  • It does not happen if WasmEnableThreads = false
  • Sometimes it is enough to click a link Counter, sometimes the issue happens after one click, sometimes I need to click several times
  • The issue itself does not bother me because it happens once and var max = JS.CallApply<int>("Math.max", numbers); calculates after this issue correctly but I can not use Shared Worker because of this issue. I see that CallApply is used when I inject the WebWorkerService.
  • It seems to me it is connected to the server prerender

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions