Skip to content

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

dotnet new web -o hello
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

1
2
3
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
1
2
3
cd hello
dotnet build
ls -lh bin/Debug/net7.0/*.wasm
run
cd hello
wasmtime bin/Debug/net7.0/hello.wasm --tcplisten localhost:8080

aspnet_wasmtime_00