Skip to content

Commit 8ab7c07

Browse files
committed
fix AllowSynchronousIO required
1 parent e6f08b5 commit 8ab7c07

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

src/WebApiContrib.Core.Formatter.Protobuf/ProtobufInputFormatter.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Threading.Tasks;
34
using Microsoft.AspNetCore.Mvc.Formatters;
45
using Microsoft.Net.Http.Headers;
@@ -31,15 +32,19 @@ public static RuntimeTypeModel Model
3132
get { return model.Value; }
3233
}
3334

34-
public override Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
35+
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
3536
{
3637
var type = context.ModelType;
3738
var request = context.HttpContext.Request;
3839
MediaTypeHeaderValue requestContentType = null;
3940
MediaTypeHeaderValue.TryParse(request.ContentType, out requestContentType);
4041

41-
object result = Model.Deserialize(context.HttpContext.Request.Body, null, type);
42-
return InputFormatterResult.SuccessAsync(result);
42+
MemoryStream stream = new MemoryStream();
43+
await request.Body.CopyToAsync(stream);
44+
45+
stream.Position = 0;
46+
object result = Model.Deserialize(stream, null, type);
47+
return await InputFormatterResult.SuccessAsync(result);
4348
}
4449

4550
protected override bool CanReadType(Type type)
Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.IO;
23
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Http;
35
using Microsoft.AspNetCore.Mvc.Formatters;
46
using Microsoft.Net.Http.Headers;
57
using ProtoBuf.Meta;
@@ -9,15 +11,15 @@ namespace WebApiContrib.Core.Formatter.Protobuf
911
public class ProtobufOutputFormatter : OutputFormatter
1012
{
1113
private readonly ProtobufFormatterOptions _options;
12-
13-
public ProtobufOutputFormatter(ProtobufFormatterOptions protobufFormatterOptions)
14-
{
15-
ContentType = "application/x-protobuf";
16-
_options = protobufFormatterOptions ?? throw new ArgumentNullException(nameof(protobufFormatterOptions));
17-
foreach (var contentType in protobufFormatterOptions.SupportedContentTypes)
18-
{
19-
SupportedMediaTypes.Add(new MediaTypeHeaderValue(contentType));
20-
}
14+
15+
public ProtobufOutputFormatter(ProtobufFormatterOptions protobufFormatterOptions)
16+
{
17+
ContentType = "application/x-protobuf";
18+
_options = protobufFormatterOptions ?? throw new ArgumentNullException(nameof(protobufFormatterOptions));
19+
foreach (var contentType in protobufFormatterOptions.SupportedContentTypes)
20+
{
21+
SupportedMediaTypes.Add(new MediaTypeHeaderValue(contentType));
22+
}
2123
}
2224

2325
private static Lazy<RuntimeTypeModel> model = new Lazy<RuntimeTypeModel>(CreateTypeModel);
@@ -36,12 +38,16 @@ private static RuntimeTypeModel CreateTypeModel()
3638
return typeModel;
3739
}
3840

39-
public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
41+
public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
4042
{
4143
var response = context.HttpContext.Response;
42-
43-
Model.Serialize(response.Body, context.Object);
44-
return Task.FromResult(response);
44+
45+
MemoryStream stream = new MemoryStream();
46+
Model.Serialize(stream, context.Object);
47+
48+
stream.Position = 0;
49+
var sr = new StreamReader(stream);
50+
await response.WriteAsync(sr.ReadToEnd());
4551
}
4652
}
4753
}

0 commit comments

Comments
 (0)