diff --git a/frameworks/CSharp/sisk/sisk-cadente/Program.cs b/frameworks/CSharp/sisk/sisk-cadente/Program.cs index 267877ee584..f198260cbde 100644 --- a/frameworks/CSharp/sisk/sisk-cadente/Program.cs +++ b/frameworks/CSharp/sisk/sisk-cadente/Program.cs @@ -1,44 +1,50 @@ -using System.Net; +using System.Buffers.Text; +using System.Net; using System.Text; using System.Text.Json; using Sisk.Cadente; -HttpHost.QueueSize = 4096; - -var host = new HttpHost ( new IPEndPoint ( IPAddress.Any, 8080 ) ); -host.ContextCreated += Host_ContextCreated; +using var host = new HttpHost ( new IPEndPoint ( IPAddress.Any, 8080 ) ); +host.Handler = new DefaultHandler(); host.Start (); Thread.Sleep ( Timeout.Infinite ); -void Host_ContextCreated ( HttpHost sender, HttpHostContext session ) { - var request = session.Request; - - if (request.Path == "/plaintext") { - SerializePlainTextResponse ( session.Response ); - } - else if (request.Path == "/json") { - SerializeJsonResponse ( session.Response ); - } - else { - session.Response.StatusCode = 404; - } -} - -static void SerializePlainTextResponse ( HttpHostContext.HttpResponse response ) { - - var messageBytes = Encoding.UTF8.GetBytes ( "Hello, World!" ); - response.Headers.Add ( new HttpHeader ( "Content-Type", "text/plain; charset=UTF-8" ) ); - response.ResponseStream = new MemoryStream ( messageBytes ); -} +class DefaultHandler : HttpHostHandler +{ + public override async Task OnContextCreatedAsync(HttpHost host, HttpHostContext context) + { + var request = context.Request; + var response = context.Response; + + if (request.Path == "/plaintext") + { + var contentBytes = Encoding.UTF8.GetBytes("Hello, World!"); + + await SerializeResponseAsync(response, contentBytes, "text/plain; charset=utf-8"); + } + else if (request.Path == "/json") + { + var contentBytes = JsonSerializer.SerializeToUtf8Bytes(new + { + message = "Hello, World!" + }); + + await SerializeResponseAsync(response, contentBytes, "application/json; charset=utf-8"); + } + else + { + response.StatusCode = 404; + } + } -static void SerializeJsonResponse ( HttpHostContext.HttpResponse response ) { + static async ValueTask SerializeResponseAsync(HttpHostContext.HttpResponse response, Memory content, string contentType) + { + response.Headers.Add(new HttpHeader("Content-Type", contentType)); + response.Headers.Add(new HttpHeader("Content-Length", content.Length.ToString())); - var contentBytes = JsonSerializer.SerializeToUtf8Bytes ( new { - message = "Hello, World!" - } ); - - response.Headers.Add ( new HttpHeader ( "Content-Type", "application/json" ) ); - response.ResponseStream = new MemoryStream ( contentBytes ); + using var responseStream = await response.GetResponseStreamAsync(); + await responseStream.WriteAsync(content); + } } \ No newline at end of file diff --git a/frameworks/CSharp/sisk/sisk-cadente/sisk.csproj b/frameworks/CSharp/sisk/sisk-cadente/sisk.csproj index c72c3f1faee..12f1a5d7c2c 100644 --- a/frameworks/CSharp/sisk/sisk-cadente/sisk.csproj +++ b/frameworks/CSharp/sisk/sisk-cadente/sisk.csproj @@ -9,7 +9,7 @@ - + \ No newline at end of file diff --git a/frameworks/CSharp/sisk/sisk.dockerfile b/frameworks/CSharp/sisk/sisk.dockerfile index 2a23e04743f..6509bffbd8a 100644 --- a/frameworks/CSharp/sisk/sisk.dockerfile +++ b/frameworks/CSharp/sisk/sisk.dockerfile @@ -9,6 +9,10 @@ RUN dotnet restore -r linux-musl-x64 COPY sisk/ . RUN dotnet publish -c release -o /app -r linux-musl-x64 +ENV DOTNET_GCDynamicAdaptationMode=0 +ENV DOTNET_ReadyToRun=0 +ENV DOTNET_HillClimbing_Disable=1 + # final stage/image FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime WORKDIR /app