Skip to content

Conversation

@ilonatommy
Copy link
Member

Add WebWorker support to Blazor project templates

This PR adds a --webworker parameter to the Blazor project templates (blazor and blazorwasm), enabling developers to create Blazor applications with Web Worker support out of the box, to offload heavy tasks.

Description

New template parameter:

--webworker - Adds a WorkerClient project and sample ImageProcessor page demonstrating Web Worker capabilities

Usage:

# Create a Blazor Web App with WebWorker support
dotnet new blazor --webworker

# Create a Blazor WebAssembly standalone app with WebWorker support  
dotnet new blazorwasm --webworker

For basic scenarios, user does not need to edit the web worker library (BlazorWebCSharp.1.WorkerClient) contents, they only edit:

  • The parameters passed to InvokeAsync method:
var processedPixels = await WorkerClient.InvokeAsync(
                "BlazorWebCSharp._1.Client.Worker.GrayscaleWorker.ApplyGrayscale",
                originalPixels,
                _intensity);`
  • Create their own heavy-job c# code, similar to GrayscaleWorker.ApplyGrayscale that is passed to the WorkerClient invocation methods.

The basic scenario supports bytes[] return type.

Fixes dotnet/runtime#95452

@ilonatommy ilonatommy self-assigned this Jan 6, 2026
@ilonatommy ilonatommy added the area-blazor Includes: Blazor, Razor Components label Jan 6, 2026
- BlazorWebCSharp.Client.csproj.in: Change #if (UseWebWorker) to <!--#if (WebWorker) --> (XML comment syntax)
- BlazorWebCSharp.1.sln: Already changed UseWebWorker to WebWorker

The template engine requires the parameter name 'WebWorker' (not the computed symbol 'UseWebWorker') for preprocessor directives to work correctly.
…sproj.in and add BOM to Razor files

1. ComponentsWebAssembly-CSharp.csproj.in: Add AllowUnsafeBlocks and ProjectReference for WebWorker
2. ImageProcessor.razor: Add UTF-8 BOM to both blazor and blazorwasm template versions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[browser][doc] how to start dotnet on WebWorker + demo

1 participant