/ICUMonitor

Sample .NET Application on Enarx

Primary LanguageC#MIT LicenseMIT

ICUMonitor: a sample .NET application on Enarx

This is a sample application, based on Steve Sanderson's Greenhouse Monitor, showing a typical ASP.NET Core code, including nontrivial features like WebSockets (via SignalR) and gRPC-Web, can work unmodified when built as a WASI-compliant .wasm file and run inside wasmtime and enarx.

Install the prerequisites

  • Install .NET 7
    • Download the dotnet-install scripts
    • On Debian, install the following packages: sudo apt-get update && sudo apt-get install build-essential cmake npm mono-devel dotnet --list-sdks
    • Run sudo chmod +x ./dotnet-install.sh
    • Install the following .NET version: ./dotnet-install.sh --channel 7.0 --version 7.0.100-preview.5.22307.18
    • Export paths: export DOTNET_ROOT=$HOME/.dotnet and export PATH=$PATH:$HOME/.dotnet:$HOME/.dotnet/tools
  • Install Node.js to build the Svelte UI
  • Install Wasmtime or Enarx to run the .NET application

Clone and build ICUMonitor

  • Clone the ICUMonitor repository:
    • git clone https://github.com/enarx/ICUMonitor.git
  • Build the Svelte UI
    • cd ICUMonitor/src/ICUMonitor/UI
    • npm install
    • npm run build
    • cd ../../..
  • Build the .NET application
    • cd src/ICUMonitor
    • dotnet build

Run ICUMonitor

  • cd src/ICUMonitor (unless you're already there)
  • Run on dotnet:
    • dotnet run
    • Or, if you're using Visual Studio, press Ctrl+F5
  • Run on wasmtime:
    • wasmtime bin/Debug/net7.0/ICUMonitor.wasm --tcplisten 0.0.0.0:5000
  • Run on enarx:
    • enarx run --wasmcfgfile ./Enarx.toml ./bin/Debug/net7.0/ICUMonitor.wasm
  • Browse to http://localhost:5000
  • Remember to do a dotnet build each time you make further code changes
  • This assumes wasmtime or enarx are available on your system PATH

Send some simulated sensor data via gRPC-Web

  • Leave the ICUMonitor app running, and open a new terminal window
  • cd src/SensorSimulator
  • dotnet run

Attach a debugger

  • Start the application using this command:
    • wasmtime bin/Debug/net7.0/ICUMonitor.wasm --tcplisten 0.0.0.0:8001 --tcplisten 0.0.0.0:11000 --env DEBUGGER_FD=3
    • Open VS Code inside the src/ICUMonitor directory (e.g., run code . from there)
    • Ensure you have the Mono Debugger extension installed
    • Set a breakpoint somewhere in the .NET code
    • Go into VS Code's "Run and debug" tool and click Attach
    • You should see the console output Accepted connection from client, socket fd=5 and then, shortly after, Now listening on...
    • At this point, if you do something to cause the breakpoint to be hit, you should see it in VS Code

Caveats

The Wasi.Sdk is a very early experimental preview, and many things aren't yet implemented. In particular, .NET's garbage collection is not enabled at all, so over time the memory usage will grow indefinitely until the application terminates. This happens after handling several hundred HTTP requests. This is obviously something we intend to fix soon.

Another issue you may encounter is that wasmtime's sock_accept support has some bugs. For example if a client disconnects ungracefully while a TCP connection is open, then the wasmtime process will terminate (this is particularly the case on Windows).

About the included package binaries

Normally people don't bundle the .nupkg package binaries with their application sources, so you may be wondering why there are ~15 MB of binaries in the packages/ directory in this repo. It's simply because the version of Wasi.Sdk that supports debugging isn't yet published to the public NuGet feed. Once the latest package builds are published, it would not be necessary to have the packages/ directory in this repo at all.