Skip to content
Closed
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
18 changes: 15 additions & 3 deletions samples/calculator/Adder/Adder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>

<!-- Need kebab case -->
<TargetName>adder</TargetName>
<TargetName>adder2</TargetName>
<WitGeneratedFilesRoot>generated/wit</WitGeneratedFilesRoot>
</PropertyGroup>

<ItemGroup>
Expand All @@ -21,7 +22,18 @@
</ItemGroup>

<ItemGroup>
<Wit Update="calculator.wit" World="computer" />
<!-- <Wit Update="calculator.wit" World="computer" /> -->
<Wit Remove="**\*.wit" />
<Wit Include="wit" World="computer" />
</ItemGroup>


<Target Name="CopyPolicyYamlToPublishDir" AfterTargets="Publish">
<PropertyGroup>
<PolicyYamlSource>$(ProjectDir)policy.yaml</PolicyYamlSource>
<PolicyYamlDest>$(PublishDir)$(TargetName).policy.yaml</PolicyYamlDest>
</PropertyGroup>
<Copy SourceFiles="$(PolicyYamlSource)" DestinationFiles="$(PolicyYamlDest)"
SkipUnchangedFiles="true" Condition="Exists('$(PolicyYamlSource)')" />
</Target>

</Project>
126 changes: 121 additions & 5 deletions samples/calculator/Adder/OperationsImpl.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,130 @@
namespace ComputerWorld.wit.exports.example.calculator;
using System;
using ComputerWorld.wit.imports.wasi.http.v0_2_0; // May be generated if wasi:http present; harmless if unused
using static ComputerWorld.wit.imports.wasi.http.v0_2_0.ITypes;

namespace ComputerWorld.wit.exports.example.calculator;

// Implementation of operations interface generated from WIT
public class OperationsImpl : IOperations
{
public static int Add(int left, int right)
public static int Add(int left, int right) => left + right;

public static string ToUpper(string input) => input.ToUpperInvariant();

// Renamed to match WIT change: get-repo(owner, name)
public static string GetRepo(string owner, string name)
{
return left + right;
// Performs a simple unauthenticated GET to the GitHub repository metadata endpoint.
// Parameters map directly to the GitHub URL path segments.
try
{
if (string.IsNullOrWhiteSpace(owner) || string.IsNullOrWhiteSpace(name))
return "{\"error\":\"missing-owner-or-name\"}";
var safeOwner = owner.Trim();
var safeName = name.Trim();
// Very basic validation to avoid path injection
if (safeOwner.Contains('/') || safeName.Contains('/'))
return "{\"error\":\"invalid-slash\"}";
var url = $"https://api.github.com/repos/{safeOwner}/{safeName}";

// Empty headers collection (GitHub typically requires a User-Agent, but per request no headers are passed).
// Fields headers;
// try
// {
// headers = Fields.FromList(new System.Collections.Generic.List<(string, byte[])>());
// }
// catch (WitException<HeaderError> hex)
// {
// return $"{{\"error\":\"header-build\",\"detail\":\"{hex.TypedValue.Tag}\"}}";
// }

var headers = new Fields();
try {
// GitHub API recommends providing a User-Agent; also add Accept for clarity.
headers.Set("User-Agent", new System.Collections.Generic.List<byte[]> { System.Text.Encoding.UTF8.GetBytes("componentize-dotnet-sample/1.0") });
headers.Set("Accept", new System.Collections.Generic.List<byte[]> { System.Text.Encoding.UTF8.GetBytes("application/vnd.github+json") });
} catch (WitException<HeaderError>) { /* non-fatal if immutable/forbidden */ }
var req = new OutgoingRequest(headers);
req.SetMethod(Method.Get());

var uri = new Uri(url);
req.SetScheme(uri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase) ? Scheme.Https() : Scheme.Http());
req.SetAuthority(uri.Authority);
req.SetPathWithQuery(uri.PathAndQuery);

RequestOptions? options = null;
var future = OutgoingHandlerInterop.Handle(req, options);

IncomingResponse? incoming = null;
const int timeoutMs = 5000; // total budget
var sw = System.Diagnostics.Stopwatch.StartNew();
int delay = 10; // start with 10ms, exponential backoff up to 250ms
while (sw.ElapsedMilliseconds < timeoutMs)
{
var resultOpt = future.Get();
if (resultOpt != null)
{
var outer = resultOpt.Value;
if (outer.IsOk)
{
var inner = outer.AsOk;
if (inner.IsOk)
{
incoming = inner.AsOk;
}
else
{
var err = inner.AsErr;
return $"{{\"error\":\"http-transport\",\"code\":\"{err.Tag}\"}}";
}
}
break;
}
System.Threading.Thread.Sleep(delay);
Copy link

@SingleAccretion SingleAccretion Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may need to poll the response (future.Subscribe + Poll) instead of simply waiting.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsturtevant posted this sample in #98 that uses PollWasiEventLoopUntilResolved. It also appears that HttpClient API can be used. That would be amazing. Going to try out https://github.com/jsturtevant/csharp-components-demos/blob/main/console/Program.cs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, If you are doing outbound http HttpClient should work!

delay = Math.Min(delay * 2, 250);
}

if (incoming == null)
{
return $"{{\"error\":\"timeout\",\"elapsedMs\":{sw.ElapsedMilliseconds}}}";
}

int status = incoming.Status();
string body = string.Empty;
try
{
var bodyRes = incoming.Consume();
var stream = bodyRes.Stream();
var bytes = new System.Collections.Generic.List<byte>();
while (true)
{
var chunk = stream.Read(1024);
if (chunk.Length == 0)
break;
bytes.AddRange(chunk);
}
body = System.Text.Encoding.UTF8.GetString(bytes.ToArray());
stream.Dispose();
bodyRes.Dispose();
}
finally
{
incoming.Dispose();
future.Dispose();
}

if (status >= 200 && status < 300) return body;
return $"{{\"error\":\"http-status\",\"status\":{status},\"body\":{EscapeForJson(body)} }}";
}
catch (Exception ex)
{
return $"{{\"error\":\"exception\",\"message\":\"{Escape(ex.Message)}\"}}";
}
}

public static string ToUpper(string input)
private static string Escape(string s) => s.Replace("\\", "\\\\").Replace("\"", "\\\"");
private static string EscapeForJson(string s)
{
return input.ToUpperInvariant();
return "\"" + Escape(s) + "\"";
}
}
14 changes: 0 additions & 14 deletions samples/calculator/Adder/calculator.wit

This file was deleted.

139 changes: 139 additions & 0 deletions samples/calculator/Adder/generated/wit/Computer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Generated by `wit-bindgen` 0.41.0. DO NOT EDIT!
// <auto-generated />
#nullable enable
using System.Runtime.InteropServices;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace ComputerWorld {

public interface IComputerWorld {
}

public readonly struct None {}

[StructLayout(LayoutKind.Sequential)]
public readonly struct Result<TOk, TErr>
{
public readonly byte Tag;
private readonly object value;

private Result(byte tag, object value)
{
Tag = tag;
this.value = value;
}

public static Result<TOk, TErr> Ok(TOk ok)
{
return new Result<TOk, TErr>(Tags.Ok, ok!);
}

public static Result<TOk, TErr> Err(TErr err)
{
return new Result<TOk, TErr>(Tags.Err, err!);
}

public bool IsOk => Tag == Tags.Ok;
public bool IsErr => Tag == Tags.Err;

public TOk AsOk
{
get
{
if (Tag == Tags.Ok)
{
return (TOk)value;
}

throw new ArgumentException("expected k, got " + Tag);
}
}

public TErr AsErr
{
get
{
if (Tag == Tags.Err)
{
return (TErr)value;
}

throw new ArgumentException("expected Err, got " + Tag);
}
}

public class Tags
{
public const byte Ok = 0;
public const byte Err = 1;
}
}

public class Option<T> {
private static Option<T> none = new ();

private Option()
{
HasValue = false;
}

public Option(T v)
{
HasValue = true;
Value = v;
}

public static Option<T> None => none;

[MemberNotNullWhen(true, nameof(Value))]
public bool HasValue { get; }

public T? Value { get; }
}

public class WitException: Exception {
public object Value { get; }
public uint NestingLevel { get; }

public WitException(object v, uint level)
{
Value = v;
NestingLevel = level;
}
}

public class WitException<T>: WitException {
public T TypedValue { get { return (T)this.Value;} }

public WitException(T v, uint level) : base(v!, level)
{
}
}

public static class InteropReturnArea
{
[InlineArray(2)]
[StructLayout(LayoutKind.Sequential, Pack = 4)]
internal struct ReturnArea
{
private uint buffer;

internal unsafe nint AddressOfReturnArea()
{
return (nint)Unsafe.AsPointer(ref buffer);
}
}

[ThreadStatic]
[FixedAddressValueType]
internal static ReturnArea returnArea = default;
}

namespace exports {

public static class ComputerWorld
{
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Generated by `wit-bindgen` 0.41.0. DO NOT EDIT!
// <auto-generated />
#nullable enable

namespace ComputerWorld.wit.exports.example.calculator;

public interface IOperations {
static abstract int Add(int left, int right);

static abstract string ToUpper(string input);

static abstract string GetRepo(string owner, string name);

}
Loading
Loading