NetWasm README

The project's README from github.com/zion-sati/NetWasm. As one markdown file: README.md.

NetWasm logo

NetWasm

Independent .NET for WebAssembly and WASI.
Write C#. Use your NuGet packages. Deploy small, standalone Wasm.

NetWasm is an independent compiler, CoreLib, runtime and SDK that compiles C# and .NET CIL directly to WebAssembly: one .wasm file holding your program, the parts of .NET it uses and a garbage collector. It does not carry the desktop CLR, Mono, dotnet.js or application DLLs into production. It is a compiler, not a UI framework: for browser UI, call the DOM from C# or use NetWasm.UI.

Star NetWasm on GitHub Publication Compiler coverage License: Community + MIT

Compile C# to WebAssembly · Try it in the browser · Get started · See supported APIs · Read the documentation

Pre-1.0, usable today. Packages are on NuGet.org, and every release passes NetWasm's full compiler test suite. A minor release can still change APIs and build output, and its notes list every breaking change. Everything you deploy (CoreLib, the runtime, the JavaScript host) is MIT; the compiler is free for individuals, open source, education and organizations with fewer than 250 employees and under USD 10 million in revenue. See License.

Is it a fit?

Good fit Not a fit
C# logic in a web page or a Web Worker, called from JavaScript Run-time reflection over members, dynamic and loading assemblies at run time, which NetWasm leaves out by design: compiling only the code a program can reach is what keeps it small
HTTP services and functions for WASI hosts such as Wasmtime, Spin and wasmCloud Libraries built on run-time reflection, such as Entity Framework
Command-line tools and plugins that run in a sandbox ASP.NET Core, whose shared framework NetWasm does not provide
NuGet packages whose reachable code avoids run-time reflection: 56 of 100 popular packages run unchanged Managed threads, parallel work and child processes

Get started

Install the .NET SDK, 10.0.303 or newer, then:

dotnet new install NetWasm.Templates
dotnet new netwasm-app -n Hello
cd Hello
dotnet run

It prints 42. dotnet publish -c Release -o publish/local writes Hello.wasm, which any WASI 0.3 host runs, and a launcher for Node.js:

wasmtime run publish/local/Hello.wasm
node publish/local/netwasm.host.mjs

For a web page, add --host page to dotnet new; dotnet run then serves the page on http://localhost:5080/. The first restore downloads the compiler, the linker and Binaryen for your machine as a NuGet package, so there is nothing else to install. Getting started goes step by step.

Quick look

It is ordinary C#. A page's C# exports methods to JavaScript and imports JavaScript functions, with .NET's own attributes:

using System.Runtime.InteropServices.JavaScript;

public static partial class PageExports
{
    [JSImport("show", "app.host")]
    private static extern void Show(string message);

    [JSExport("greet")]
    public static string Greet(string name)
    {
        var message = $"Hello, {name}, from C#.";
        Show(message);
        return message;
    }
}

The build turns it into a typed JavaScript module for the page:

import { open } from "./browser/Hello.mjs";

const app = await open();
app.greet("browser"); // runs the C# above, which calls show() on the page

and show is your own JavaScript, with the DOM at hand:

export function show(message) {
  document.querySelector("#result").textContent = message;
}

JavaScript interop · Web Workers · HTTP services · What a project can build

Runtime async, faster than desktop .NET

.NET 11's runtime async drops the state machine C# writes for every async method: the runtime suspends and resumes the method itself. NetWasm compiles your async methods and .NET's own libraries this way, with nothing to set, in the browser and under WASI. On .NET 11, Blazor WebAssembly runs on Mono, which cannot run runtime-async methods (dotnet/runtime#137984).

On 23 async workloads, from awaits and Task.Yield to channels, pipes, streams, JSON and Deflate, NetWasm runs about 1.5 times as fast as desktop .NET 11's JIT and more than five times as fast as Mono AOT. Desktop compiles them with runtime async too, and its JIT runs optimized code from the first call. Share of the other runtime's time that NetWasm takes, geometric mean, lower is faster:

Async workload Scenarios Mono AOT Desktop .NET 11 JIT Desktop .NET 11 NativeAOT
Awaits, yields, WhenAll, iterators, exceptions 11 0.09 0.49 0.57
CoreLib's stream loops and SemaphoreSlim 6 0.31 0.84 0.98
JSON, pipes, channels and Deflate 6 0.39 0.80 1.14
All 23 0.18 0.64 0.79

Five thousand exceptions, each thrown ten async methods down and caught at the top, take 4.1 ms, against 9.2 ms on desktop NativeAOT and 440 ms on Mono AOT. Across the 171 async micro-benchmarks, NetWasm takes 0.74 of desktop's time and 0.12 of Mono AOT's. Every scenario, and how it was measured

C# at less than half Rust's Wasm size

NetWasm + precise GC: 26,444 bytes
Rust 1.100.0 size-oriented profile: 63,528 bytes

For the same print-42 workload, NetWasm's result includes the runtime and precise Compact garbage collector. Both artifacts are standalone WASI 0.3 components, built the way a developer gets them out of the box: each toolchain's standard library and ordinary build, with no hand-added tools.

58.4% smaller raw. 42.2% smaller with Brotli-11.

Wasm footprint landscape with raw and Brotli-11 sizes. NetWasm with precise GC is 26,444 raw and 11,285 compressed bytes; Rust is 63,528 and 19,519; Rust's WASI 0.2 build, shown for context, is 53,382 and 16,725; Kotlin/Wasm is 79,293 and 22,655; TinyGo is 93,043 and 26,931. NetWasm and Rust are the matched WASI 0.3 comparison; Kotlin and TinyGo are contextual Preview 1 references.

Toolchain Raw Wasm Brotli 11 Comparison role
NetWasm, Compact GC 26,444 B 11,285 B WASI 0.3 component; precise GC included
Rust 1.100.0, size profile 63,528 B 19,519 B Matched WASI 0.3 component reference
Rust 1.100.0, size profile 53,382 B 16,725 B WASI 0.2 component, for context
Kotlin 2.4.0 wasmWasi 79,293 B 22,655 B P1 context; GC supplied by the host engine
TinyGo 0.39.0 93,043 B 26,931 B P1 context; precise GC configured

NetWasm's figure is the template's default Release publish; Rust's adds Cargo's documented size settings. A real program is bigger and still one file: the HTTP service template, with .NET's HTTP stack, is a 1,043,482-byte component, 251,134 bytes with Brotli, that wasmtime serve runs as it is. Sources, settings and reproduction

Your NuGet packages, native ones included

A NetWasm project targets net10.0-netwasm, a .NET platform target framework like Blazor's net10.0-browser. NuGet restores each package's net10.0, net9.0, net8.0 or .NET Standard build, newest first, as it does for any .NET 10 project, so a package needs no NetWasm build of its own. Of 100 of the most-downloaded nuget.org packages that ship a .NET Standard build, 56 run their README quick start unchanged, printing exactly what desktop .NET prints: Azure.Core, the AWS SDK's core, MSAL, CsvHelper, MongoDB.Driver, gRPC's client, RabbitMQ.Client, Open XML SDK, BouncyCastle, SSH.NET and HtmlAgilityPack among them. Most of the rest stop on run-time reflection, which NetWasm leaves out to keep programs small, and several have a source-generated replacement that runs.

Packages that carry native code come along too. The static archives SkiaSharp and HarfBuzzSharp ship for .NET's WebAssembly SDKs link into the program as they are, and Avalonia 12's headless platform renders the same frame as on desktop .NET, through Skia, with no change to Avalonia.

Every package's result and the replacements · How packages restore, and what stops them

More than three times as fast as Mono AOT

Across 2,389 micro-benchmark cases, dotnet/performance's and NetWasm's own, NetWasm runs more than three times as fast as Mono AOT (geometric mean), compiled from the same C# and run by the same Wasmtime, and at about two-thirds of the speed of desktop .NET 11's JIT, which runs native code outside any sandbox. Every case

A real application too: David Fowler's DoomSharp renders about 1.4 times as fast as its Mono AOT build in the browser, from a ninth of the download.

DoomSharp in headless Chromium on an Apple M1 Max: rendering 120 frames, warmed up, takes 1,655 ms with NetWasm and 2,343 ms with its Blazor WebAssembly build, which is Mono AOT; the page downloads 0.45 MiB with NetWasm and 4.03 MiB with Mono AOT, every file compressed at Brotli quality 11, not counting the game's WAD. Lower is better.

On allocation and garbage-collection workloads NetWasmGC is 2 to 5 times as fast as Mono AOT, and beats native code too:

Workload NetWasm, NetWasmGC Mono AOT, .NET 11 Desktop .NET 11 JIT Desktop .NET 11 NativeAOT
Full transaction 25.9 ms 56.9 ms 49.5 ms 32.4 ms
Retained object graph 4.3 ms 14.1 ms 9.7 ms 7.5 ms
UI churn 5.1 ms 26.5 ms 8.5 ms 6.6 ms

Medians of 15 runs on an Apple M1 Max, under Wasmtime 50.0.1, each checked against desktop .NET's result. Versions, settings and reproduction

Choose your garbage collector

Four collectors, one property. Compact, the default, is a size-first, precise, non-moving mark-and-sweep collector; its name refers to implementation size, not moving heap compaction. NetWasmGC trades a few kilobytes for faster allocation and collection, and NetWasmGen adds generations to it. Every tracing collector follows compiler-published roots and reference layouts, without conservative scanning.

Collector Best fit Raw component Brotli 11
Compact (default) Small applications and low-churn UI 26,444 B 11,285 B
NetWasmGC Higher allocation throughput 30,449 B 13,011 B
NetWasmGen A large live heap with short-lived garbage on top: up to 3 times NetWasmGC's speed 33,705 B 14,425 B
Bump Bounded, one-shot jobs 21,842 B 9,261 B

NetWasmGen is NetWasmGC with generations. Most of its collections look only at the objects allocated since the last one and at the old objects the program stored a reference into, so a program that keeps a large set of objects alive while short-lived ones come and go runs up to three times as fast as under NetWasmGC, and binary-trees 1.5 to 1.8 times as fast. Choose it for a service answering requests against in-memory state, a game or simulation making garbage every frame, or a compiler building and discarding trees; stay on NetWasmGC for short runs and for data that dies all at once. Across 57 garbage-collection programs it takes 0.93 of NetWasmGC's time. When to choose NetWasmGen

Select a collector in the executable project's .csproj:

<PropertyGroup>
  <NetWasmGarbageCollector>NetWasmGen</NetWasmGarbageCollector>
</PropertyGroup>

The values are Compact, NetWasm, NetWasmGen and Bump; leaving the property unset selects Compact. With any of the tracing collectors, a page, a worker and a component's event loop collect while the program waits, so collections land between interactions rather than inside them. Libraries remain collector-neutral, and all four collectors are packaged for wasm32 and wasm64. See Choosing a garbage collector for the measurements, the tradeoffs and diagnostics.

Built with NetWasm

What else it does

C# 15 · runtime async · JavaScript modules and Web Workers · native archives · WIT/WASI 0.3 · HTTP/JSON/XML/Regex · TLS and HTTP/2 · expression trees · reflection-free DI · TUnit/VSTest · Avalonia · wasm64 · four garbage collectors

How it stays small

Roslyn produces CIL. NetWasm compiles the reachable program directly to Wasm, specializes its generics and links only the runtime support it needs.

How it works · How JSON and optional features affect size

Requirements

.NET SDK 10.0.303 or newer, including .NET 11; C# 15 needs the .NET 11 SDK
Build machines Linux x64 and ARM64, macOS ARM64, Windows x64 and ARM64
Browsers Chrome and Edge 137, Firefox 131, Safari 18.4, or newer
WASI hosts Wasmtime 50, Spin, wasmCloud and other WASI 0.3 hosts
Node.js 24.15 or newer, when you host the output in your own Node.js

Compatibility and versioning

Documentation

Current boundary and the road to 1.0

NetWasm 1.0 means a useful, documented and stable smaller platform, not desktop .NET in Wasm. Run-time reflection over members, dynamic and loading assemblies at run time are left out by design and will stay out. Managed threading, child processes and named-culture globalization are outside the profile too. Most such operations still compile, and throw PlatformNotSupportedException when they run. The normal portable output is a wasm32 WASI 0.3 component; raw wasm32 and wasm64 modules serve JavaScript and custom hosts.

Done: WASI 0.3, a fast incremental development loop, NuGet packages under net10.0-netwasm, and runtime async. Next: globalization modes and optional culture data, expression-tree completion, a stable platform and runtime contract, and production diagnostics with broader host qualification.

Supported APIs and limitations · The 1.0 roadmap

Changelog

Version What's new
0.16.0 Runtime async in WebAssembly, faster than desktop .NET on async workloads; debugging by C# line; calls that dispatch only to the types that reach them; large applications compile in less memory.
0.15.2 HTTP service handlers that finish under the JavaScript host.
0.15.1 Loops in exception handlers, Math as .NET computes it, and a JavaScript host that releases what calls held.

Full changelog

Packages

Compiler, SDK, runtime and hosting packages

NetWasm.Compiler.Browser · NetWasm.Compiler.Tasks · NetWasm.Hosting · NetWasm.Hosting.Build · NetWasm.Ref · NetWasm.Runtime.Pack · NetWasm.Runtime.Wasm32 · NetWasm.Runtime.Wasm64 · NetWasm.Sdk · NetWasm.Templates · NetWasm.Testing.VSTest · NetWasm.Toolchain · NetWasm.Wit.Bindings.Tool

Ported-library packages · TUnit packages

License

The compiler and the surrounding tools that create your deployable package, with their tests, use the NetWasm Community License 1.0. Everything else is MIT, subject to preserved upstream notices, including all code you need to deploy an application: CoreLib, runtime libraries, the JavaScript host, templates and generated support code. The Compact and NetWasmGC collectors are Apache-2.0.

The Community License is free for individuals, education, open-source projects, evaluation, and small and medium-sized businesses: any organization with fewer than 250 employees and less than USD 10,000,000 in annual revenue. Larger organizations pay for internal commercial use by sponsoring a matching GitHub Sponsors tier, from USD 149 a month for five developers up to USD 2,499 a month for unlimited developers. OEM and Commercial Offering use require a separate agreement. Generated applications may use their authors' chosen licenses. The sponsorship itself is the license: there is no license key, sign-in, activation, telemetry or technical enforcement.

Licensing guide · Third-party notices · Commercial licensing: [email protected]

Bring a workload

Start with Console.WriteLine(42), then try the smallest real workload where deployment size, sandboxing or portability matters. If something is missing, open an issue. Concrete workloads are how the 1.0 support boundary gets decided.

NetWasm (zion-sati/NetWasm)

Searching Database…

What is this?NetWasm is an independent compiler, CoreLib, runtime and SDK that compiles C# and .NET CIL directly to small standalone WebAssembly with WASI, without carrying the desktop CLR, Mono, dotnet.js or application DLLs into production.
Is this a full .NET runtime for Wasm, or a separate smaller platform?It is a new, separate .NET-like platform built specifically for Wasm, not desktop .NET squeezed into a Wasm sandbox; it has its own closed-world compiler, CoreLib and runtime.
How do I build and run a NetWasm project?Install .NET SDK 10.0.303 or newer and build the project through the ordinary dotnet CLI, then run the published WASI Preview 2 component with a compatible host such as Wasmtime.
Do I need Emscripten, Git or Python installed separately?No, the SDK restores its own pinned Node, LLD and Binaryen tools through NuGet, so an ordinary app does not require a separate Emscripten, Git or Python installation.
What .NET features are off-limits right now?General runtime reflection, the dynamic keyword, runtime assembly loading, managed threading, and general System.IO.File/Directory/FileStream, raw sockets and subprocess APIs are not yet supported; existing NuGet packages only work if their reachable code fits the supported profile.
How small is a real NetWasm output compared to Rust or Blazor?For the same print-42 workload, a NetWasm WASI P2 component with precise GC is 47,798 bytes, about 10.9% smaller raw than a size-optimized Rust build (53,635 bytes), and roughly 257x smaller than an equivalent empty-UI Blazor WebAssembly app (12.27 MB).
Which garbage collector should I pick?Compact is the default size-first precise collector for small apps and low-churn UI; NetWasmGC favors higher allocation throughput, Bump suits bounded one-shot jobs, and Boehm offers conservative collector compatibility. You select one via a property in the .csproj.
Can I write libraries that work both in Wasm and on desktop .NET?Yes, the project template dual-targets netwasm0.1 and net10.0 so NuGet packages ship assets for both platforms, and NetWasm.Libraries provides ported framework packages for this purpose.
Is NetWasm production-ready yet?It is pre-1.0: the supported profile is usable today, but source, package, target-profile and ABI contracts may still change between minor releases, and features like broader System.IO and globalization support are planned for 0.9.0 and 1.0.
Creator
zion-sati
Size
39.5 MB
Stars
362
Age
3mo
Commits · 90 Days