Skip to content

.NET Web Worker template update to Blazor Web Worker template#66070

Open
ilonatommy wants to merge 11 commits intodotnet:mainfrom
ilonatommy:web-worker-template-updates
Open

.NET Web Worker template update to Blazor Web Worker template#66070
ilonatommy wants to merge 11 commits intodotnet:mainfrom
ilonatommy:web-worker-template-updates

Conversation

@ilonatommy
Copy link
Copy Markdown
Member

@ilonatommy ilonatommy commented Mar 30, 2026

  • Remove the license headers from generated template files
  • Remove or condense the heavy XML doc comments on WebWorkerClient
  • The DisposeAsync method in WebWorkerClient catches a JSDisconnectedException with the comment "Circuit disconnected, worker is already gone", but circuits are specific to Blazor Server-

Done in efc73fb. See #65937 (comment)

  • Consider adding an initialization timeout for the worker in case the runtime fails to load silently.

If we do it for create we could also do it for invoke, they have similar level of risk. If user's exported method never resolves, it will also hang forever. We should align both to use cancellation tokens and timeouts.
Done in 2119dd8.

  • The parseIfJson logic in dotnet-web-worker-client.js seems potentially problematic if a worker method returns a string that happens to look like JSON. Consider either always deserializing or never parsing.

Done in 4d00bab. See: #65937 (comment).

  • Consider adding an InvokeVoidAsync method to WebWorkerClient

Done in 036d9cf.

This invocation pattern seems very specific to the host app being a Blazor WebAssembly app. Could we make the .NET Web Worker independent of Blazor?

After offline discussion we decided to make the supported template more Blazor-focused, with default support of web worker running the C# code located in web worker project (not only in the host project). This update contains:

  • Changing the name to Blazor Web Worker class library template (most of the changes in this PR is moving files around after the rename).
  • Changing the templating engine command to dotnet new blazorwebworker.
  • Adding assemblyName parameter to initialization method that is populated by default with web worker's assembly name in WebWorkerClient. In JS we load exports from both: host project and from web worker project.
  • Adding a very simple C# file with JSExported method, WorkerMethods.cs. It's a boilerplate for removal but blazor web template also ships such an easy-to-remove files like Counter.razor. That file documents how to call exported method from host project and shows the place where to embed other methods for the web worker.
  • An integral part of this update is a pending change to docs that would adjust our React&WebWorker article and Blazor&WebWorker articles. It will be done after this PR is merged.

Fixes #65937

@ilonatommy ilonatommy self-assigned this Mar 30, 2026
@ilonatommy ilonatommy added the area-blazor Includes: Blazor, Razor Components label Mar 30, 2026
Copilot AI review requested due to automatic review settings March 30, 2026 09:49
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Cleans up the .NET Web Worker project template output by removing license headers from generated files and simplifying the WebWorkerClient template code comments while adding a void-invocation helper.

Changes:

  • Removed .NET Foundation license headers from generated Web Worker template JS/C# files.
  • Condensed WebWorkerClient’s heavy XML documentation into shorter inline comments.
  • Added WebWorkerClient.InvokeVoidAsync and updated the JSDisconnectedException comment to be WebWorker-appropriate.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/ProjectTemplates/Web.ProjectTemplates/content/WebWorker-CSharp/wwwroot/dotnet-web-worker.js Removes license header from generated worker bootstrap script.
src/ProjectTemplates/Web.ProjectTemplates/content/WebWorker-CSharp/wwwroot/dotnet-web-worker-client.js Removes license header from generated client-side worker wrapper module.
src/ProjectTemplates/Web.ProjectTemplates/content/WebWorker-CSharp/WebWorkerClient.cs Simplifies template docs, adds InvokeVoidAsync, updates disconnect comment.
Comments suppressed due to low confidence (1)

src/ProjectTemplates/Web.ProjectTemplates/content/WebWorker-CSharp/WebWorkerClient.cs:33

  • The PR description says "Fixes #65937", but this change set appears to address only a subset of the issue items (license header removal, doc/comment cleanup, and adding InvokeVoidAsync). Items like an initialization timeout for worker startup and the parseIfJson behavior are still present. Consider updating the PR description (e.g., remove "Fixes"/mark as partial) or including the remaining fixes before merging so the issue isn’t closed prematurely.
    public static async Task<WebWorkerClient> CreateAsync(IJSRuntime jsRuntime)
    {
        await using var module = await jsRuntime.InvokeAsync<IJSObjectReference>(
            "import", "./_content/Company.WebWorker1/dotnet-web-worker-client.js");

@ilonatommy ilonatommy force-pushed the web-worker-template-updates branch from 9fe7142 to efc73fb Compare March 30, 2026 09:53
Copy link
Copy Markdown
Member

@danroth27 danroth27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! A few comments.

@ilonatommy ilonatommy changed the title .NET Web Worker template cleanup .NET Web Worker template update to Blazor Web Worker template Apr 2, 2026
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.

.NET Web Worker template cleanup

3 participants