You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[.net11] Use the new webworker template instead of samples (#36871)
* Update docs for .net11 -> now it's easier to use the new template than refer to sample apps etc.
* Updates
---------
Co-authored-by: Luke Latham <1622880+guardrex@users.noreply.github.com>
Copy file name to clipboardExpand all lines: aspnetcore/blazor/blazor-with-dotnet-on-web-workers.md
+199-1Lines changed: 199 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,12 @@
1
1
---
2
2
title: ASP.NET Core Blazor with .NET on Web Workers
3
+
ai-usage: ai-assisted
3
4
author: guardrex
4
5
description: Learn how to use Web Workers to enable JavaScript to run on separate threads that don't block the main UI thread for improved app performance in a Blazor WebAssembly app.
Modern Blazor WebAssembly apps often handle CPU-intensive work alongside rich UI updates. Tasks such as image processing, document parsing, or data crunching can easily freeze the browser's main thread. Web Workers let you push that work to a background thread. Combined with the .NET WebAssembly runtime, you can keep writing C# while the UI stays responsive.
20
21
22
+
:::moniker range=">= aspnetcore-11.0"
23
+
24
+
The `webworker` project template provides built-in scaffolding for running .NET code in a Web Worker. The template generates the required JavaScript worker scripts and a C# `WebWorkerClient` class, which removes the need to write the interop layer manually. To learn about Web Workers with React, see <xref:client-side/dotnet-on-webworkers>.
25
+
26
+
> [!NOTE]
27
+
> The `webworker` template isn't limited to Blazor. The template works with any .NET WebAssembly host, including standalone `wasmbrowser` apps and custom JavaScript frontends, such as React or vanilla JS. In non-Blazor scenarios, import the template's JavaScript client (`dotnet-web-worker-client.js`) directly from your entry point and call `[JSExport]` methods without the Blazor-specific C# `WebWorkerClient` class.
28
+
29
+
## Create the projects
30
+
31
+
Create a Blazor WebAssembly app and a .NET Web Worker class library:
32
+
33
+
```dotnetcli
34
+
dotnet new blazorwasm -n SampleApp
35
+
dotnet new webworker -n WebWorker
36
+
```
37
+
38
+
Add a project reference from the app to the worker library:
Enable the <xref:Microsoft.Build.Tasks.Csc.AllowUnsafeBlocks> property in the app's project file (`SampleApp.csproj`), which is required for [`[JSExport]` attribute](xref:System.Runtime.InteropServices.JavaScript.JSExportAttribute) usage:
48
+
49
+
```xml
50
+
<PropertyGroup>
51
+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
52
+
</PropertyGroup>
53
+
```
54
+
55
+
> [!WARNING]
56
+
> The JS interop API requires enabling <xref:Microsoft.Build.Tasks.Csc.AllowUnsafeBlocks>. Be careful when implementing your own unsafe code in .NET apps, which can introduce security and stability risks. For more information, see [Unsafe code, pointer types, and function pointers](/dotnet/csharp/language-reference/unsafe-code).
57
+
58
+
## Define worker methods
59
+
60
+
Worker methods are `static` methods marked with [`[JSExport]`](xref:System.Runtime.InteropServices.JavaScript.JSExportAttribute) in a `static partial class`. Define them in the main application project because the assembly name must match the one used by the worker runtime.
61
+
62
+
Due to `[JSExport]` limitations, worker methods can only return primitives or strings. For complex types, serialize to JSON before returning. The `WebWorkerClient` automatically deserializes JSON results.
Inject `IJSRuntime` and use `WebWorkerClient.CreateAsync` to create a worker instance. The client manages the JavaScript messaging layer on your behalf.
91
+
92
+
`Pages/Home.razor`:
93
+
94
+
```razor
95
+
@page "/"
96
+
@using WebWorker
97
+
@implements IAsyncDisposable
98
+
@inject IJSRuntime JSRuntime
99
+
100
+
<PageTitle>Home</PageTitle>
101
+
102
+
<h1>Web Worker demo</h1>
103
+
104
+
<button class="btn btn-primary" @onclick="CallWorker" disabled="@(worker is null)">
*`CreateAsync`: Initializes the worker and waits for the .NET runtime to be ready inside the worker thread.
210
+
*`InvokeAsync<TResult>`: Calls a `[JSExport]` method on the worker by its full name (`Namespace.ClassName.MethodName`) and returns the deserialized result. JSON string results are automatically parsed into `TResult`.
211
+
*`DisposeAsync`: Terminates the worker and releases resources. Use `await using` or call explicitly.
212
+
213
+
:::moniker-end
214
+
215
+
:::moniker range="< aspnetcore-11.0"
216
+
21
217
The guidance in this article mirrors the concepts from the React-focused *.NET on Web Workers* walkthrough, but adapts every step to a Blazor frontend. It highlights the same QR-code generation scenario implemented in this repository. To learn about Web Workers with React, see <xref:client-side/dotnet-on-webworkers>.
22
218
23
219
## Sample app
@@ -314,6 +510,8 @@ public partial class Home : ComponentBase
314
510
* Move long-running workflows into dedicated worker instances per feature area.
315
511
* Explore shared array buffers or Atomics when you need higher-throughput synchronization between Blazor and workers.
Copy file name to clipboardExpand all lines: aspnetcore/client-side/dotnet-on-webworkers.md
+7-1Lines changed: 7 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,12 @@
1
1
---
2
2
title: .NET on Web Workers
3
+
ai-usage: ai-assisted
3
4
author: guardrex
4
5
description: Learn how to use Web Workers to enable JavaScript to run on separate threads that don't block the main UI thread for improved app performance in a React app.
5
6
monikerRange: '>= aspnetcore-8.0'
6
7
ms.author: wpickett
7
8
ms.custom: mvc
8
-
ms.date: 11/20/2025
9
+
ms.date: 03/13/2026
9
10
uid: client-side/dotnet-on-webworkers
10
11
---
11
12
# .NET on Web Workers
@@ -20,6 +21,11 @@ Modern web apps often require intensive computational tasks that can block the m
20
21
21
22
This approach is particularly valuable when you need to perform complex calculations, data processing, or business logic without requiring direct DOM manipulation. Instead of rewriting algorithms in JS, you can maintain your existing .NET codebase and execute it efficiently in the background while your React.js frontend remains responsive.
22
23
24
+
> [!TIP]
25
+
> Starting with .NET 11, the `webworker` project template (`dotnet new webworker`) scaffolds the JavaScript worker scripts and interop boilerplate for you. The template works with any .NET WebAssembly host—Blazor, standalone `wasmbrowser` apps, and custom JavaScript frontends like React. Import the template's JavaScript client (`dotnet-web-worker-client.js`) directly from your app's entry point to get started. For the Blazor-specific integration that includes a C# `WebWorkerClient` class, see <xref:blazor/blazor-web-workers>.
26
+
27
+
This article demonstrates the manual approach for React.js frontends using a standalone .NET WebAssembly project.
28
+
23
29
## Sample app
24
30
25
31
Explore a complete working implementation in the [Blazor samples GitHub repository](https://github.com/dotnet/blazor-samples). The sample is available for .NET 10 or later and named `DotNetOnWebWorkersReact`.
0 commit comments