| title | Getting Started |
|---|---|
| section | AngleSharp.Wasm |
AngleSharp.Wasm currently targets modern .NET runtimes and is available for .NET 8 and .NET 10.
You need to have AngleSharp installed already. This could be done via NuGet:
Install-Package AngleSharpIf you want JavaScript-side access to window.WebAssembly, you also need a scripting setup (for example, AngleSharp.Js) that discovers exported DOM members from loaded assemblies, or is given the registration of this library (see below).
The simplest way of integrating AngleSharp.Wasm to your project is by using NuGet. You can install AngleSharp.Wasm by opening the package manager console (PM) and typing in the following statement:
Install-Package AngleSharp.WasmYou can also use the graphical library package manager ("Manage NuGet Packages for Solution"). Searching for "AngleSharp.Wasm" in the official NuGet online feed will find this library.
To use AngleSharp.Wasm you need to add it to your Configuration coming from AngleSharp itself.
If you just want a configuration that works you should use the following code:
var config = Configuration.Default
.WithWasm(); // from AngleSharp.WasmThis will register everything related for running WebAssembly.
If your Wasm module imports host functions, register import providers:
var config = Configuration.Default
.WithWasm()
.WithWasmImports(context =>
{
return new[]
{
new WasmImportFunction(
moduleName: "host",
functionName: "answer",
parameterTypes: Array.Empty<WasmValueType>(),
resultTypes: new[] { WasmValueType.Int32 },
callback: _ => 41),
};
});AngleSharp.Js finds the DOM of this library by reflecting over its DOM attributes by default. A trimmed or natively compiled (AoT) application can instead hand it the registration generated for this library at build time, AngleSharp.Wasm.WasmDom.Register (a DomRegistration of AngleSharp). It declares the WebAssembly namespace with its interfaces (WebAssembly.Module, WebAssembly.Memory, ...):
var config = Configuration.Default
.WithWasm()
.WithJs(WasmDom.Register);The registration describes the version of the library in use, so it never has to be kept in sync by hand. Several libraries are combined with DomRegistrations.Combine.
WithWasm()registers a default Wasmtime-backed runtime factory.WithWasmImports(...)can provide host imports during instantiation.- The DOM surface exposes
WebAssembly.compile(...)andWebAssembly.instantiate(...). Modulemetadata APIs are available:exports()imports()customSections(sectionName)
Instancesupports:exportsobject access by nameinvoke(exportName, ...args)helper
- Export descriptors expose spec-style fields (
name,kind).
AngleSharp.Wasm currently implements a pragmatic subset of the WebAssembly JS API.
- Synchronous bridge calls are used (
compile/instantiateare not Promise-based APIs). - Streaming APIs synchronously buffer AngleSharp response streams. Compile options are forwarded to capable runtimes, but Wasmtime 44 does not support JavaScript string builtins or imported string constants.
Instance.exportsfunction members are wrapper objects requiring.invoke(...).- Compile, link, and execution failures are projected as
CompileError,LinkError, andRuntimeErrorobjects. - Exported memories, tables, globals, and tags have object projections; other non-function exports remain descriptors (
name,kind). - Host-created exceptions support tag identity and typed payload inspection. The default Wasmtime 44 backend does not expose native Tag or Exception handles, so runtime-thrown Wasm exceptions are not yet projected.
customSections(...)returns payload bytes (byte[]) mapped from custom sections.- Runtime invocation/import marshaling is currently focused on numeric value kinds (
i32,i64,f32,f64).
These limitations are intentional for the current release scope and can be evolved incrementally.