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
Copy file name to clipboardExpand all lines: aspnetcore/blazor/blazor-with-dotnet-on-web-workers.md
+37-29Lines changed: 37 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ description: Learn how to use Web Workers to enable JavaScript to run on separat
6
6
monikerRange: '>= aspnetcore-8.0'
7
7
ms.author: wpickett
8
8
ms.custom: mvc
9
-
ms.date: 03/13/2026
9
+
ms.date: 04/07/2026
10
10
uid: blazor/blazor-web-workers
11
11
---
12
12
# ASP.NET Core Blazor with .NET on Web Workers
@@ -21,18 +21,18 @@ Modern Blazor WebAssembly apps often handle CPU-intensive work alongside rich UI
21
21
22
22
:::moniker range=">= aspnetcore-11.0"
23
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>.
24
+
The Blazor Web Worker project template (`dotnet new blazorwebworker`) provides built-in scaffolding for running .NET code in a Web Worker in a Blazor WebAssembly app. The template generates the required JavaScript worker scripts, a C# `WebWorkerClient` class, and a starter `WorkerMethods.cs` file, 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
25
26
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.
27
+
> In .NET 11 and later, the Blazor Web Worker template (`blazorwebworker`) is intended for Blazor WebAssembly scenarios. For React or other custom JavaScript frontends, use the manual approach in <xref:client-side/dotnet-on-webworkers>.
28
28
29
29
## Create the projects
30
30
31
31
Create a Blazor WebAssembly app and a .NET Web Worker class library:
32
32
33
33
```dotnetcli
34
34
dotnet new blazorwasm -n SampleApp
35
-
dotnet new webworker -n WebWorker
35
+
dotnet new blazorwebworker -n WebWorker
36
36
```
37
37
38
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:
47
+
The template already enables the <xref:Microsoft.Build.Tasks.Csc.AllowUnsafeBlocks> property in the worker project and includes a starter `WorkerMethods.cs` file with a [`[JSExport]` attribute](xref:System.Runtime.InteropServices.JavaScript.JSExportAttribute)example. Add or update methods in the worker project as needed.
48
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).
49
+
Worker methods are `static` methods marked with [`[JSExport]`](xref:System.Runtime.InteropServices.JavaScript.JSExportAttribute) in a `static partial class`.
57
50
58
-
## Define worker methods
51
+
Due to `[JSExport]` limitations, worker methods should use JS interop-friendly types such as primitives and strings. For complex data, serialize to JSON in the worker and deserialize it in the Blazor app.
59
52
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.
*`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`.
216
+
*`CreateAsync`: Initializes the worker and waits for the .NET runtime to be ready inside the worker thread. The `assemblyName` parameter defaults to the worker project's assembly.
217
+
*`InvokeAsync<TResult>`: Calls a `[JSExport]` method on the worker by its full name (`AssemblyName.ClassName.MethodName`) and returns the result.
218
+
*`InvokeVoidAsync`: Calls a `[JSExport]` method that doesn't return a value.
211
219
*`DisposeAsync`: Terminates the worker and releases resources. Use `await using` or call explicitly.
0 commit comments