Skip to content

Commit ac4d497

Browse files
committed
use Async I/O for SwaggerMiddleware fix #226
1 parent fb8be77 commit ac4d497

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

sandbox/Sandbox.NetCoreServer/Program.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
#pragma warning disable CS0219
1+
#pragma warning disable CS0219
22

33
using Grpc.Core;
44
using Grpc.Core.Logging;
55
using MagicOnion;
66
using MagicOnion.Client;
77
using MagicOnion.HttpGateway.Swagger;
88
using MagicOnion.Server;
9-
using MagicOnion.Server.EmbeddedServices;
109
using MagicOnion.Utils;
1110
using MessagePack;
1211
using MagicOnion.Hosting;
@@ -61,15 +60,18 @@ static async Task Main(string[] args)
6160
// test webhost
6261

6362
// NuGet: Microsoft.AspNetCore.Server.Kestrel
64-
var webHost = new WebHostBuilder()
63+
var webHost = Host.CreateDefaultBuilder()
6564
.ConfigureServices(collection =>
6665
{
6766
// Add MagicOnionServiceDefinition for reference from Startup.
6867
collection.AddSingleton<MagicOnionServiceDefinition>(magicOnionHost.Services.GetService<MagicOnionHostedServiceDefinition>().ServiceDefinition);
6968
})
70-
.UseKestrel()
71-
.UseStartup<Startup>()
72-
.UseUrls("http://localhost:5432")
69+
.ConfigureWebHost(web =>
70+
{
71+
web.UseKestrel()
72+
.UseStartup<Startup>()
73+
.UseUrls("http://localhost:5432");
74+
})
7375
.Build();
7476

7577
await Task.WhenAll(webHost.RunAsync(), magicOnionHost.RunAsync());

src/MagicOnion.HttpGateway/MagicOnionSwaggerMiddleware.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using MagicOnion.HttpGateway.Swagger;
1+
using MagicOnion.HttpGateway.Swagger;
22
using MagicOnion.Server;
33
using Microsoft.AspNetCore.Http;
44
using System.Collections.Generic;
@@ -24,7 +24,7 @@ public MagicOnionSwaggerMiddleware(RequestDelegate next, IReadOnlyList<MethodHan
2424
this.options = options;
2525
}
2626

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

4747
var myAssembly = typeof(MagicOnionSwaggerMiddleware).GetTypeInfo().Assembly;
@@ -53,13 +53,14 @@ public Task Invoke(HttpContext httpContext)
5353
if (stream == null)
5454
{
5555
// not found, standard request.
56-
return next(httpContext);
56+
await next(httpContext);
57+
return;
5758
}
5859

5960
httpContext.Response.Headers["Content-Type"] = new[] { mediaType };
6061
httpContext.Response.StatusCode = 200;
6162
var response = httpContext.Response.Body;
62-
stream.CopyTo(response);
63+
await stream.CopyToAsync(response);
6364
}
6465
else
6566
{
@@ -72,26 +73,24 @@ public Task Invoke(HttpContext httpContext)
7273
{
7374
using (var ms = new MemoryStream())
7475
{
75-
stream.CopyTo(ms);
76+
await stream.CopyToAsync(ms);
7677
bytes = options.ResolveCustomResource(path, ms.ToArray());
7778
}
7879
}
7980

8081
if (bytes == null)
8182
{
8283
// not found, standard request.
83-
return next(httpContext);
84+
await next(httpContext);
85+
return;
8486
}
8587

8688
httpContext.Response.Headers["Content-Type"] = new[] { mediaType };
8789
httpContext.Response.StatusCode = 200;
8890
var response = httpContext.Response.Body;
89-
response.Write(bytes, 0, bytes.Length);
91+
await response.WriteAsync(bytes, 0, bytes.Length);
9092
}
9193
}
92-
93-
94-
return EmptyTask;
9594
}
9695

9796
static string GetMediaType(string path)

0 commit comments

Comments
 (0)