Run ASP.Net inside WASM with WASI and WasmTime
About this post
- Created: 2022 November 9th
.NET 7 is Available and there is a preview WASI support for dotNet Core and ASP.Net. It's very easy to getting started.
Setup
Install .NET 7
- Go on https://dotnet.microsoft.com/en-us/download/dotnet/7.0 and download the appropriate SDK for your OS
- Install it
Install WasmTime
| curl https://wasmtime.dev/install.sh -sSf | bash
|
Generate a new ASP.Net project
output |
---|
| hello
βββ appsettings.Development.json
βββ appsettings.json
βββ hello.csproj
βββ obj
β βββ hello.csproj.nuget.dgspec.json
β βββ hello.csproj.nuget.g.props
β βββ hello.csproj.nuget.g.targets
β βββ project.assets.json
β βββ project.nuget.cache
βββ Program.cs
βββ Properties
βββ launchSettings.json
|
Install the dotNet WASI support for the project
| cd hello
dotnet add package Wasi.Sdk --prerelease
dotnet add package Wasi.AspNetCore.Server.Native --prerelease
|
Warning
The released packages exist but the build will fail if you use it
Change the source code of Program.cs
Update `Program.cs` |
---|
| using System.Runtime.InteropServices;
var builder = WebApplication.CreateBuilder(args).UseWasiConnectionListener();
var app = builder.Build();
app.MapGet("/", () => {
return $"π Hello, World! π π₯οΈ: {RuntimeInformation.OSArchitecture} β³: {DateTime.UtcNow.ToLongTimeString()} (UTC)";
});
app.Run();
|
Build and run
build |
---|
| cd hello
dotnet build
ls -lh bin/Debug/net7.0/*.wasm
|
run |
---|
| cd hello
wasmtime bin/Debug/net7.0/hello.wasm --tcplisten localhost:8080
|

Some readings on the same topic