Skip to content

Commit f729880

Browse files
committed
Update after run through
Signed-off-by: James Sturtevant <jstur@microsoft.com>
1 parent c3db27e commit f729880

File tree

1 file changed

+28
-11
lines changed
  • component-model/src/language-support

1 file changed

+28
-11
lines changed

component-model/src/language-support/csharp.md

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,36 @@ world example {
3939
}
4040
```
4141

42-
In the .csproj project file, add a new `<ItemGroup>`:
42+
In the `adder.csproj` project file, add a new `<ItemGroup>`:
4343

4444
```xml
4545
<ItemGroup>
4646
<Wit Update="add.wit" World="example" />
4747
</ItemGroup>
4848
```
4949

50-
If you try to build the project with `dotnet build`, you'll get an error like "The name
50+
Since this component will only export a function dotnet considers this a library project. Let's update
51+
the `<OutputType>` to be a library in the `adder.csproj`:
52+
53+
```xml
54+
<OutputType>Library</OutputType>
55+
```
56+
57+
And remove the `Program.cs` file:
58+
59+
```bash
60+
rm Program.cs
61+
```
62+
63+
At this point, if you try to build the project with `dotnet build`, you'll get an error like "The name
5164
'ExampleWorldImpl' does not exist in the current context". This is because you've said you'll
5265
provide an implementation, but haven't yet done so. To fix this, add the following code to your
53-
project:
66+
project in a file called `adder.cs`:
5467

5568
```csharp
5669
namespace ExampleWorld;
5770

58-
public class ExampleWorldImpl : IOperations
71+
public class ExampleWorldImpl : IExampleWorld
5972
{
6073
public static uint Add(uint x, uint y)
6174
{
@@ -100,7 +113,7 @@ world hostapp {
100113

101114
If you peek at the bindings, you'll notice that we now implement a class for the `add` interface
102115
rather than for the `example` world. This is a consistent pattern. As you export more interfaces
103-
from your world, you implement more classes. Our add example gets the slight update of:
116+
from your world, you implement more classes. Our `adder.cs` example gets the slight update of:
104117

105118
```csharp
106119
namespace ExampleWorld.wit.exports.example.component;
@@ -135,9 +148,11 @@ our `adder` component and call the `add` function. We will later compose this co
135148
the `adder` library component we just built.
136149

137150
Now we will be taking the `adder` component and executing it from another WebAssembly component.
138-
`dotnet new componentize.wasi.cli` creates a new project that creates an executable.
151+
`dotnet new componentize.wasi.cli` creates a new project that creates an executable.
152+
Back out of the current project and create a new one:
139153

140154
```sh
155+
cd ..
141156
dotnet new componentize.wasi.cli -o host-app
142157
cd host-app
143158
```
@@ -180,8 +195,8 @@ Modify `Program.cs` to look like this:
180195
// example.component refers to the package name defined in the WIT file.
181196
using HostappWorld.wit.imports.example.component;
182197

183-
var left = 1;
184-
var right = 2;
198+
uint left = 1;
199+
uint right = 2;
185200
var result = AddInterop.Add(left, right);
186201
Console.WriteLine($"{left} + {right} = {result}");
187202
```
@@ -216,11 +231,11 @@ You can also automate the process by adding the following to your `host-app.cspr
216231
<Target Name="ComposeWasmComponent" AfterTargets="Publish">
217232
<PropertyGroup>
218233
<EntrypointComponent>bin/$(Configuration)/$(TargetFramework)/wasi-wasm/native/host-app.wasm</EntrypointComponent>
219-
<DependencyComponent>../example/bin/$(Configuration)/$(TargetFramework)/wasi-wasm/native/adder.wasm</DependencyComponent>
234+
<DependencyComponent>../adder/bin/$(Configuration)/$(TargetFramework)/wasi-wasm/native/adder.wasm</DependencyComponent>
220235
</PropertyGroup>
221236

222237
<MakeDir Directories="dist" />
223-
<Exec Command="$(WacExe) plug $(EntrypointComponent) --plug $(DependencyComponent)" -o dist/main.wasm />
238+
<Exec Command="$(WacExe) plug $(EntrypointComponent) --plug $(DependencyComponent) -o dist/main.wasm" />
224239
</Target>
225240
```
226241

@@ -229,6 +244,8 @@ Run `dotnet build` again you will have a composed component in `./dist/main.wasm
229244
Then you can run the composed component:
230245

231246
```sh
232-
wasmtime run main.wasm
247+
wasmtime run ./dist/main.wasm
233248
1 + 2 = 3
234249
```
250+
251+
Checkout the [componentize-dotnet docs](https://github.com/bytecodealliance/componentize-dotnet) for more configurations options.

0 commit comments

Comments
 (0)