Skip to content

Update docs to use the template to get started #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 52 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,58 +26,24 @@ With this package, you can add one NuGet reference. The build output is fully AO

If you don't already have it, install [.NET 9+ SDK](https://dotnet.microsoft.com/en-us/download/dotnet/9.0)

### 2. Create a project and add BytecodeAlliance.Componentize.DotNet.Wasm.SDK package
### 2. Install template and build

* `dotnet new console -o MyApp`
* `cd MyApp`

Create a `nuget.config` file and add the `dotnet-experimental` package source for the `NativeAOT-LLVM` dependency:

* `dotnet new nugetconfig`
* Add these keys to `nuget.config` after `<clear />`:

```xml
<add key="dotnet-experimental" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-experimental/nuget/v3/index.json" />
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
```

Add the `componentize-dotnet` package:

`dotnet add package BytecodeAlliance.Componentize.DotNet.Wasm.SDK --prerelease`

Add the platform specific LLVM package:

```
## On Linux
dotnet add package runtime.linux-x64.microsoft.dotnet.ilcompiler.llvm --prerelease

## or

## On Windows
dotnet add package runtime.win-x64.microsoft.dotnet.ilcompiler.llvm --prerelease
```

Edit the `.csproj` file, adding the following inside the `<PropertyGroup>`:

```
<RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
<UseAppHost>false</UseAppHost>
<PublishTrimmed>true</PublishTrimmed>
<InvariantGlobalization>true</InvariantGlobalization>
<SelfContained>true</SelfContained>
```bash
dotnet new install BytecodeAlliance.Componentize.DotNet.Templates
dotnet new componentize.wasi.cli -o MyApp
dotnet build MyApp
```

Now you can `dotnet build` to produce a `.wasm` file using NativeAOT compilation.

### 4. Run the WebAssembly binary
### 3. Run the WebAssembly binary

If you have a recent version of [wasmtime](https://github.com/bytecodealliance/wasmtime/releases) on your path, you can now run

```bash
wasmtime bin\Debug\net8.0\wasi-wasm\native\MyApp.wasm
wasmtime ./MyApp/bin/Debug/net9.0/wasi-wasm/publish/MyApp.wasm
Hello world from compontize-dotnet!
```

(if needed, replace `MyApp.wasm` with the actual name of your project)
(if needed, replace `MyApp` with the actual name of your project)

## Creating a WASI 0.2 component, including WIT support
Lastest version of NativeAOT compiler package and the mono support in dotnet 9-preview 7 build native wasi 0.2 components with no additional tools.
Expand Down Expand Up @@ -285,6 +251,49 @@ The calculator example above works easily because it doesn't need to allocate me

If you get a build error along the lines of _failed to encode a component from module ... module does not export a function named `cabi_realloc`_ then check you have remembered to add this line.

### Create a project manually

* `dotnet new console -o MyApp`
* `cd MyApp`

Create a `nuget.config` file and add the `dotnet-experimental` package source for the `NativeAOT-LLVM` dependency:

* `dotnet new nugetconfig`
* Add these keys to `nuget.config` after `<clear />`:

```xml
<add key="dotnet-experimental" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-experimental/nuget/v3/index.json" />
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
```

Add the `componentize-dotnet` package:

`dotnet add package BytecodeAlliance.Componentize.DotNet.Wasm.SDK --prerelease`

Add the platform specific LLVM package:

```
## On Linux
dotnet add package runtime.linux-x64.microsoft.dotnet.ilcompiler.llvm --prerelease

## or

## On Windows
dotnet add package runtime.win-x64.microsoft.dotnet.ilcompiler.llvm --prerelease
```

Edit the `.csproj` file, adding the following inside the `<PropertyGroup>`:

```
<RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
<UseAppHost>false</UseAppHost>
<PublishTrimmed>true</PublishTrimmed>
<InvariantGlobalization>true</InvariantGlobalization>
<SelfContained>true</SelfContained>
```

Now you can `dotnet build` to produce a `.wasm` file using NativeAOT compilation.

### Troubleshooting

#### Imports Wrong Type
Expand Down
2 changes: 1 addition & 1 deletion templates/content/wasi-cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, world from compontize-dotnet!");
Console.WriteLine("Hello world from compontize-dotnet!");
2 changes: 1 addition & 1 deletion templates/content/wasi-cli/wasi-cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BytecodeAlliance.Componentize.DotNet.Wasm.SDK" Version="0.5.0-preview00008" />
<PackageReference Include="BytecodeAlliance.Componentize.DotNet.Wasm.SDK" Version="0.6.0-preview00009" />
<PackageReference Condition="'$(platform)' == 'Windows_NT'" Include="runtime.win-x64.microsoft.dotnet.ilcompiler.llvm" Version="10.0.0-alpha.1.25118.1" />
<PackageReference Condition="'$(platform)' == 'linux'" Include="runtime.linux-x64.microsoft.dotnet.ilcompiler.llvm" Version="10.0.0-alpha.1.25118.1" />
</ItemGroup>
Expand Down