builder.Services.AddBlazorJSRuntime();
await builder.Build().BlazorJSRunAsync();
@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}");
}
}
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
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.