Skip to content

Commit

Permalink
use Async I/O for SwaggerMiddleware fix #226
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Jan 9, 2020
1 parent fb8be77 commit ac4d497
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
14 changes: 8 additions & 6 deletions sandbox/Sandbox.NetCoreServer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#pragma warning disable CS0219
#pragma warning disable CS0219

using Grpc.Core;
using Grpc.Core.Logging;
using MagicOnion;
using MagicOnion.Client;
using MagicOnion.HttpGateway.Swagger;
using MagicOnion.Server;
using MagicOnion.Server.EmbeddedServices;
using MagicOnion.Utils;
using MessagePack;
using MagicOnion.Hosting;
Expand Down Expand Up @@ -61,15 +60,18 @@ static async Task Main(string[] args)
// test webhost

// NuGet: Microsoft.AspNetCore.Server.Kestrel
var webHost = new WebHostBuilder()
var webHost = Host.CreateDefaultBuilder()
.ConfigureServices(collection =>
{
// Add MagicOnionServiceDefinition for reference from Startup.
collection.AddSingleton<MagicOnionServiceDefinition>(magicOnionHost.Services.GetService<MagicOnionHostedServiceDefinition>().ServiceDefinition);
})
.UseKestrel()
.UseStartup<Startup>()
.UseUrls("http://localhost:5432")
.ConfigureWebHost(web =>
{
web.UseKestrel()
.UseStartup<Startup>()
.UseUrls("http://localhost:5432");
})
.Build();

await Task.WhenAll(webHost.RunAsync(), magicOnionHost.RunAsync());
Expand Down
23 changes: 11 additions & 12 deletions src/MagicOnion.HttpGateway/MagicOnionSwaggerMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MagicOnion.HttpGateway.Swagger;
using MagicOnion.HttpGateway.Swagger;
using MagicOnion.Server;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
Expand All @@ -24,7 +24,7 @@ public MagicOnionSwaggerMiddleware(RequestDelegate next, IReadOnlyList<MethodHan
this.options = options;
}

public Task Invoke(HttpContext httpContext)
public async Task Invoke(HttpContext httpContext)
{
// reference embedded resouces
const string prefix = "MagicOnion.HttpGateway.Swagger.SwaggerUI.";
Expand All @@ -40,8 +40,8 @@ public Task Invoke(HttpContext httpContext)
var bytes = builder.BuildSwaggerJson();
httpContext.Response.Headers["Content-Type"] = new[] { "application/json" };
httpContext.Response.StatusCode = 200;
httpContext.Response.Body.Write(bytes, 0, bytes.Length);
return EmptyTask;
await httpContext.Response.Body.WriteAsync(bytes, 0, bytes.Length);
return;
}

var myAssembly = typeof(MagicOnionSwaggerMiddleware).GetTypeInfo().Assembly;
Expand All @@ -53,13 +53,14 @@ public Task Invoke(HttpContext httpContext)
if (stream == null)
{
// not found, standard request.
return next(httpContext);
await next(httpContext);
return;
}

httpContext.Response.Headers["Content-Type"] = new[] { mediaType };
httpContext.Response.StatusCode = 200;
var response = httpContext.Response.Body;
stream.CopyTo(response);
await stream.CopyToAsync(response);
}
else
{
Expand All @@ -72,26 +73,24 @@ public Task Invoke(HttpContext httpContext)
{
using (var ms = new MemoryStream())
{
stream.CopyTo(ms);
await stream.CopyToAsync(ms);
bytes = options.ResolveCustomResource(path, ms.ToArray());
}
}

if (bytes == null)
{
// not found, standard request.
return next(httpContext);
await next(httpContext);
return;
}

httpContext.Response.Headers["Content-Type"] = new[] { mediaType };
httpContext.Response.StatusCode = 200;
var response = httpContext.Response.Body;
response.Write(bytes, 0, bytes.Length);
await response.WriteAsync(bytes, 0, bytes.Length);
}
}


return EmptyTask;
}

static string GetMediaType(string path)
Expand Down

0 comments on commit ac4d497

Please sign in to comment.