Introducing dotnet-wasi-sdk
The dotnet-wasi-sdk project is an experimental Software Development Kit (SDK) designed to compile .NET Core projects into WebAssembly (WASM) files that comply with the WebAssembly System Interface (WASI). With this ability, developers can create applications that run in various WASI environments or hosts. This project, although experimental, provides a valuable tool for those interested in exploring .NET applications within the WebAssembly ecosystem.
Console Applications
Setting up a console application using dotnet-wasi-sdk is straightforward. By generating a new console project and adding the Wasi.Sdk
package, developers can build their project into a .wasm
file. After building, the .wasm
file is placed in the bin/Debug/net7.0
directory.
To execute this WASM file, ensure that a WASI runtime like Wasmtime or Wasmer is installed and accessible through the system PATH
. You can run your application using commands specific to these tools:
- With Wasmtime:
wasmtime bin/Debug/net7.0/MyFirstWasiApp.wasm
- With Wasmer:
wasmer bin/Debug/net7.0/MyFirstWasiApp.wasm
ASP.NET Core Applications
Developing ASP.NET Core applications with dotnet-wasi-sdk involves a few additional steps:
- Create a new web application project.
- Add both
Wasi.Sdk
andWasi.AspNetCore.Server.Native
packages. - Modify the
Program.cs
to set up a WASI connection listener. - Adjust
Properties/launchSettings.json
to configure theapplicationUrl
with a single HTTP listener. - Update your project file to include appropriate WASI runner arguments that match your launch settings.
This setup allows the application to run using dotnet run
, leveraging the broader functionalities of ASP.NET Core within a WASI context.
For those interested in deploying static files, you can package wwwroot
files into the WASM output, ensuring all resources are bundled together. This requires adding Wasi.AspNetCore.BundledFiles
and replacing some lines in your project settings.
What's Inside
The repository includes several key components:
Wasi.Sdk
: Facilitates the production of WASI-compliant.wasm
files, automatically managing necessary components from the WASI SDK.Wasi.AspNetCore.BundledFiles
: Provides theUseBundledStaticFiles
feature for including static files directly within the WASM file.Wasi.AspNetCore.Server.Native
: Supports running ASP.NET Core apps using WASI's networking standards, like TCP-level socket operations.
Building from Source
To build the dotnet-wasi-sdk from source, a series of prerequisites are necessary, including .NET 7, Rust, and several development tools on a Linux system or within WSL (Windows Subsystem for Linux). After installing dependencies and setting up the development environment, the SDK and associated samples can be compiled using dotnet build
or run directly.
Conclusion
dotnet-wasi-sdk provides an intriguing avenue for developers to explore the integration of .NET projects within the WebAssembly space, particularly those optimistic about WASI's expanding capabilities. While this SDK has since been superseded by the .NET SDK's wasi-experimental
workload, the principles and tools introduced remain relevant, offering a unique perspective on cross-platform application development.