Skip to content

Adds a Blazor WebAssembly (.NET 10) + Motoko Hello World template.#1359

Open
mitnick2012 wants to merge 1 commit into
dfinity:masterfrom
mitnick2012:master
Open

Adds a Blazor WebAssembly (.NET 10) + Motoko Hello World template.#1359
mitnick2012 wants to merge 1 commit into
dfinity:masterfrom
mitnick2012:master

Conversation

@mitnick2012

Copy link
Copy Markdown

Overview
The icp-cli templates library has no support for .NET/C# developers who want to
build on ICP using Blazor WebAssembly. This template fills that gap by providing
a working starter project that combines Blazor WASM (.NET 10) on the frontend with
a Motoko canister on the backend, using icp-cli for deployment. Without this template,
.NET developers face undocumented pitfalls that make getting started unnecessarily
difficult.

Requirements

  • Blazor WASM frontend that successfully calls a Motoko backend canister
  • Uses icp-cli with canister.yaml configuration (not dfx)
  • Works on local network and mainnet
  • Documents all known gotchas specific to Blazor + ICP
  • No JS interop race conditions between @dfinity/agent and Blazor runtime
  • Canister ID discovery via ic_env cookie without hardcoding

Considered Solutions

  1. ES module script (type="module") for @dfinity/agent — rejected due to browser
    deferral behavior causing a race condition where Blazor attempts JS interop before
    the module is ready, silently breaking button events.
  2. Webpack bundle loaded with defer — selected. Compiles @dfinity/agent into a
    single plain JS file exposed as window.IcpAgent. The defer attribute guarantees
    script execution order before DOMContentLoaded, so the global is always available
    when Blazor makes its first JS interop call.

Recommended Solution
Webpack + defer loading pattern, modeled after the existing Dfinity.Blazor
community library. This is the only approach that reliably solves the JS interop
race condition without adding complexity to the Blazor startup sequence. It also
bundles @dfinity/agent locally (no CDN dependency), pins the version in package.json,
and produces a single minified file that the asset canister can certify correctly.

Considerations

  • Security: No impact. The template follows the same trust model as other icp-cli
    templates — no private keys, no hardcoded canister IDs.
  • Performance: The webpack bundle adds ~271KB (uncompressed) to the frontend assets.
    This is negligible compared to the Blazor WASM runtime (~5MB+). Brotli compression
    via the asset canister reduces the bundle to ~80KB on the wire.
  • Users: Purely additive. Existing templates are unaffected. .NET developers gain
    a working starting point without having to discover undocumented pitfalls.
  • Breaking changes: None.
  • Maintenance: Low. The template depends on @dfinity/agent and the @dfinity/motoko
    recipe — both of which are maintained by DFINITY. The .NET 10 LTS support window
    runs until November 2028, so no forced upgrade in

@mitnick2012 mitnick2012 requested a review from a team as a code owner June 9, 2026 18:32
@mitnick2012

Copy link
Copy Markdown
Author

Hello @marc0olo,
Could you please review this feature?

@marc0olo

Copy link
Copy Markdown
Member

Hey @mitnick2012,

I will discuss with the team how to handle this. But I don't think we want to maintain external examples here in this repo.

In any case this would be better suited for the https://github.com/dfinity/icp-cli-templates repository. See this guide on how to create and use custom templates: https://cli.internetcomputer.org/1.0/guides/creating-templates

What I could imagine is that we provide an overview for community created templates either in the official icp-cli documentation or in the README of the icp-cli-templates repository.

I also noticed that you depend on @dfinity/agent in your example. You should migrate this to https://js.icp.build/core/latest/libs/agent

Also note that the latest Motoko recipe (v5.0.0) delegates the compilation entirely to mops build:

Best,
Marco

@mitnick2012

Copy link
Copy Markdown
Author

Thanks @marc0olo for the review I will update the MR with your remarks, and move it to template repo, my aim is to give the blazor users a starting push to build with icp

@marc0olo

marc0olo commented Jun 29, 2026

Copy link
Copy Markdown
Member

@mitnick2012 for now my recommendation is following:

  • create a "real" template as per guide above
  • create a forum post about this template, share its github repo and advertise the command how to use it

independent of that I will think about how we can provide a registry for independent templates created by the community.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants