Skip to content

Commit

Permalink
http sample
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelsavara committed Jul 2, 2024
1 parent e9cc7a0 commit 4f5c7b2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/mono/sample/wasi/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
-->
<Exec WorkingDirectory="bin/wasi-wasm/AppBundle"
Condition="'$(WasmBuildNative)' != 'true'"
Command="$(WasmtimeDir)wasmtime$(_ExeExt) $(MONO_LOG_LEVEL) --dir . dotnet.wasm $(_SampleProjectName)" IgnoreExitCode="true" />
Command="$(WasmtimeDir)wasmtime$(_ExeExt) $(MONO_LOG_LEVEL) -S http --dir . dotnet.wasm $(_SampleProjectName)" IgnoreExitCode="true" />
<Exec WorkingDirectory="bin/wasi-wasm/AppBundle"
Condition="'$(WasmBuildNative)' == 'true' and '$(WasmSingleFileBundle)' != 'true'"
Command="$(WasmtimeDir)wasmtime$(_ExeExt) $(MONO_LOG_LEVEL) --dir . dotnet.wasm" IgnoreExitCode="true" />
Command="$(WasmtimeDir)wasmtime$(_ExeExt) $(MONO_LOG_LEVEL) -S http --dir . dotnet.wasm" IgnoreExitCode="true" />
<Exec WorkingDirectory="bin/wasi-wasm/AppBundle"
Condition="'$(WasmSingleFileBundle)' == 'true'"
Command="$(WasmtimeDir)wasmtime$(_ExeExt) $(MONO_LOG_LEVEL) $([System.IO.Path]::ChangeExtension($(_SampleAssembly), '.wasm'))" IgnoreExitCode="true" />
Command="$(WasmtimeDir)wasmtime$(_ExeExt) $(MONO_LOG_LEVEL) -S http $([System.IO.Path]::ChangeExtension($(_SampleAssembly), '.wasm'))" IgnoreExitCode="true" />
</Target>

<Import Project="$(RepositoryEngineeringDir)testing\wasi-provisioning.targets" />
Expand Down
57 changes: 57 additions & 0 deletions src/mono/sample/wasi/http-p2/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Threading.Tasks;
using System.Threading;
using System.Runtime.CompilerServices;

public class Test
{
public static int Main(string[] args)
{
var task = Work();
while (!task.IsCompleted)
{
WasiEventLoop.DispatchWasiEventLoop();
}
var exception = task.Exception;
if (exception is not null)
{
throw exception;
}

return 0;
}

public static async Task Work()
{
using HttpClient client = new();
client.Timeout = Timeout.InfiniteTimeSpan;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
client.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");

var query="https://api.github.com/orgs/dotnet/repos?per_page=1";
var json = await client.GetStringAsync(query);

Console.WriteLine();
Console.WriteLine("GET "+query);
Console.WriteLine();
Console.WriteLine(json);
}

private static class WasiEventLoop
{
internal static void DispatchWasiEventLoop()
{
CallDispatchWasiEventLoop((Thread)null!);

[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "DispatchWasiEventLoop")]
static extern void CallDispatchWasiEventLoop(Thread t);
}
}
}
11 changes: 11 additions & 0 deletions src/mono/sample/wasi/http-p2/Wasip2.Http.Console.Sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<!--
<WasmSingleFileBundle>true</WasmSingleFileBundle>
<InvariantGlobalization>true</InvariantGlobalization>
-->
</PropertyGroup>

<Target Name="RunSample" DependsOnTargets="RunSampleWithWasmtime" />
</Project>

0 comments on commit 4f5c7b2

Please sign in to comment.