Packages for building .NET projects as standalone WASI-compliant modules
C#
514
21 commits
updated Mar 28, 2024
This experimental SDK is now superseded by the .NET SDK's wasi-experimental workload (which itself is still experimental). See https://github.com/dotnet/runtime/blob/main/src/mono/wasi/README.md
Everything below this point is for comparative purposes only. Please move your experiments over to use wasi-experimental instead of the packages in this repo.
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.
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,
PATHdotnet run or, if you're using Visual Studio, press Ctrl+F5Alternatively you can invoke runners like wasmtime or wasmer manually on the command line. For example,
wasmtime bin/Debug/net7.0/MyFirstWasiApp.wasmwasmer bin/Debug/net7.0/MyFirstWasiApp.wasmOther WASI hosts work similarly.
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>
Wasi.Sdk - a package that causes your build to produce a WASI-compliant .wasm file. This works by:
.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
First, build the runtime. This can take quite a long time.
git submodule update --init --recursivesudo apt-get install build-essential cmake ninja-build python python3 zlib1g-devcd modules/runtime/src/mono/wasm
make provision-wasm (takes about 2 minutes)make build-all (takes 10-15 minutes)
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:
dotnet --version should return 7.0.100-preview.4 or later)wasm32-unknown-unknown target (technically this is only needed for the CustomHost package)
rustup target add wasm32-unknown-unknowndotnet 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(top 24 of 25)
448 followers · starred Jul 2022
301 followers · starred Jun 2022
1,384 followers · starred Aug 2022
9,868 followers · starred Mar 2022
Packages for building .NET projects as standalone WASI-compliant modules
C#
514
21 commits
updated Mar 28, 2024
This experimental SDK is now superseded by the .NET SDK's wasi-experimental workload (which itself is still experimental). See https://github.com/dotnet/runtime/blob/main/src/mono/wasi/README.md
Everything below this point is for comparative purposes only. Please move your experiments over to use wasi-experimental instead of the packages in this repo.
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.
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,
PATHdotnet run or, if you're using Visual Studio, press Ctrl+F5Alternatively you can invoke runners like wasmtime or wasmer manually on the command line. For example,
wasmtime bin/Debug/net7.0/MyFirstWasiApp.wasmwasmer bin/Debug/net7.0/MyFirstWasiApp.wasmOther WASI hosts work similarly.
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>
Wasi.Sdk - a package that causes your build to produce a WASI-compliant .wasm file. This works by:
.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
First, build the runtime. This can take quite a long time.
git submodule update --init --recursivesudo apt-get install build-essential cmake ninja-build python python3 zlib1g-devcd modules/runtime/src/mono/wasm
make provision-wasm (takes about 2 minutes)make build-all (takes 10-15 minutes)
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:
dotnet --version should return 7.0.100-preview.4 or later)wasm32-unknown-unknown target (technically this is only needed for the CustomHost package)
rustup target add wasm32-unknown-unknowndotnet 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(top 24 of 25)
448 followers · starred Jul 2022
301 followers · starred Jun 2022
1,384 followers · starred Aug 2022
9,868 followers · starred Mar 2022