• This repository has been archived on 28/Mar/2024
  • Stars
    star
    519
  • Rank 84,756 (Top 2 %)
  • Language
    C#
  • Created over 2 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Packages for building .NET projects as standalone WASI-compliant modules

Experimental WASI SDK for .NET Core

Wasi.Sdk is an experimental package that can build .NET Core projects (including whole ASP.NET Core applications) into standalone WASI-compliant .wasm files. These can then be run in standard WASI environments or custom WASI-like hosts.

How to use: Console applications

dotnet new console -o MyFirstWasiApp
cd MyFirstWasiApp
dotnet add package Wasi.Sdk --prerelease
dotnet build

You'll see from the build output that this produces bin/Debug/net7.0/MyFirstWasiApp.wasm.

To run it,

  • Ensure you've installed wasmtime and it's available on your system PATH
  • Run your app via dotnet run or, if you're using Visual Studio, press Ctrl+F5

Alternatively you can invoke runners like wasmtime or wasmer manually on the command line. For example,

  • For wasmtime, run wasmtime bin/Debug/net7.0/MyFirstWasiApp.wasm
  • For wasmer, run wasmer bin/Debug/net7.0/MyFirstWasiApp.wasm

Other WASI hosts work similarly.

How to use: ASP.NET Core applications

dotnet new web -o MyWebApp
cd MyWebApp
dotnet add package Wasi.Sdk --prerelease
dotnet add package Wasi.AspNetCore.Server.Native --prerelease

Then:

  • Open your new project in an IDE such as Visual Studio or VS Code

  • Open Program.cs and change the line var builder = WebApplication.CreateBuilder(args) to look like this:

    var builder = WebApplication.CreateBuilder(args).UseWasiConnectionListener();
  • Open Properties/launchSettings.json and edit the applicationUrl value to contain only a single HTTP listener, e.g.,

    "applicationUrl": "http://localhost:8080"
  • Open your .csproj file (e.g., in VS, double-click on the project name) and, inside a <PropertyGroup>, add this:

    <WasiRunnerArgs>--tcplisten localhost:8080 --env ASPNETCORE_URLS=http://localhost:8080</WasiRunnerArgs>

    Instead of 8080, you should enter the port number found in Properties\launchSettings.json.

That's it! You can now run it via dotnet run (or in VS, use Ctrl+F5)

Optionally, to add support for bundling wwwroot files into the .wasm file and serving them:

  • Add the NuGet package Wasi.AspNetCore.BundledFiles

  • In Program.cs, replace app.UseStaticFiles(); with app.UseBundledStaticFiles();

  • In your .csproj file, add:

    <ItemGroup>
        <WasmBundleFiles Include="wwwroot\**" />
    </ItemGroup>

What's in this repo

  • Wasi.Sdk - a package that causes your build to produce a WASI-compliant .wasm file. This works by:
    • Downloading the WASI SDK, if you don't already have it
    • When your regular .NET build is done, it takes the resulting assemblies, plus the .NET runtime precompiled to WebAssembly, and uses WASI SDK to bundle them into a single .wasm file. You can optionally include other native sources such as .c files in the compilation.
  • Wasi.AspNetCore.BundledFiles - provides UseBundledStaticFiles, and alternative to UseStaticFiles, that serves static files bundled into your .wasm file. This allows you to have single-file deployment even if you have files under wwwroot or elsewhere.
  • Wasi.AspNetCore.Server.Native - a way of running ASP.NET Core on WASI's TCP-level standard networking APIs (e.g., sock_accept). These standards are quite recent and are currently only supported in Wasmtime, not other WASI hosts.

... and more

Building this repo from source

First, build the runtime. This can take quite a long time.

  • git submodule update --init --recursive
  • Do the following steps using Linux or WSL:
    • sudo apt-get install build-essential cmake ninja-build python python3 zlib1g-dev
  • cd modules/runtime/src/mono/wasm
    • make provision-wasm (takes about 2 minutes)
    • make build-all (takes 10-15 minutes)
      • If you get an error about setlocale: LC_ALL: cannot change locale then run sudo apt install language-pack-en. This only happens on very bare-bones machines.
  • cd ../wasi
    • make (takes a few minutes - there are lots of warnings like "System is unknown to cmake" and that's OK)

Now you can build the packages and samples in this repo:

  • Prerequisites
    • .NET 7 (dotnet --version should return 7.0.100-preview.4 or later)
    • Rust and the wasm32-unknown-unknown target (technically this is only needed for the CustomHost package)
  • Just use dotnet build or dotnet run on any of the samples or src projects, or open the solution in VS and Ctrl+F5 on any of the sample projects

More Repositories

1

WebWindow

.NET Core library to open native OS windows containing web UI on Windows, Mac, and Linux. Experimental.
TypeScript
1,983
star
2

CarChecker

A sample Blazor WebAssembly application that includes authentication, in-browser data storage, offline support, localization, responsive layouts, and more. For a video walkthrough, see this link:
C#
811
star
3

DotNetIsolator

A library for running isolated .NET runtimes inside .NET
C#
598
star
4

BlazeOrbital

Sample application for Blazor WebAssembly on .NET 6
C#
524
star
5

BlazorDesktop

TypeScript
286
star
6

BlazorInputFile

A file input component for Blazor applications
C#
251
star
7

presentation-2019-06-NDCOslo

NDC Oslo 2019
160
star
8

BlazorElectronExperiment.Sample

HTML
158
star
9

BlazorOnGitHubPages

A simple example of hosting a Blazor WebAssembly app on GitHub pages
HTML
105
star
10

BlazorUnitTestingPrototype

C#
93
star
11

presentation-2020-01-DotNetConf

80
star
12

wasm-component-sdk

Tooling for creating WebAssembly components from C#
C#
77
star
13

presentation-2019-10-NDCSydney

73
star
14

AudioBrowser

A media browser app using Blazor WebAssembly with .NET 7 and 8 features
C#
70
star
15

BlazorGrpcSamples

C#
61
star
16

StatefulReconnection

JavaScript
48
star
17

ProductsManager

Simple Blazor list/editor sample using .NET 7 features
JavaScript
46
star
18

MonacoRazor

A Blazor component that provides the Monaco code editor
HTML
46
star
19

presentation-2020-01-NdcBlazorComponentLibraries

Shell
46
star
20

PictureFixer

C#
44
star
21

presentation-2020-01-NDCLondon

43
star
22

BlazorComponentDemos

JavaScript
27
star
23

MyDotNet8PWA

Sample of a .NET 8 Blazor Web app configured to serve an offline-enabled Blazor PWA
HTML
24
star
24

aspnetcore-in-browser

HTML
23
star
25

presentation-2021-01-NDCLondon

22
star
26

GreenhouseMonitor

Sample application for ASP.NET Core on WASI
C#
21
star
27

RazorComponents.MaterialDesign

JavaScript
21
star
28

MinimalDotNetWasmNativeAOT

Small examples of .NET compiling to wasi-wasm via NativeAOT-LLVM
C#
15
star
29

il2wasm

Do not use
C
15
star
30

ghaction-rewrite-base-href

GitHub action to rewrite the base href in an HTML file
JavaScript
12
star
31

spiderlightning-dotnet

C
12
star
32

CircuitPersisterExample

Sample
HTML
9
star
33

RazorComponentsSentimentAnalysis

Example of combining Razor Components with ML.NET for realtime sentiment analysis while typing
C#
9
star
34

TemporaryBlazorWebNet8TemplateExperiments

Just to try out some different conventions and file structures
CSS
8
star
35

BlazorExtraLinking

Experiment with more linking on a Blazor app
HTML
6
star
36

blazor-benchmarks

JavaScript
5
star
37

AngularBasicTemplate

C#
5
star
38

CurrencyConverter

HTML
5
star
39

BlazorPerfTest

Scenarios to help define perf tuning goals
HTML
4
star
40

BlazorElectronExperiment.Packages

TypeScript
3
star
41

sample-with-caching

CSS
2
star
42

angular2-universal-patch

Updates angular2-universal to support Angular 2.3+. Will no longer be needed when Angular 4 is released.
JavaScript
2
star
43

SignalRFirefoxRepro

HTML
1
star
44

chunktest

HTML
1
star