Allow sync IO for NewtonsoftJsonOutputFormatter #8302 (#8303)

This commit is contained in:
Chris Ross 2019-03-07 19:11:35 -08:00 committed by GitHub
parent bae2f2280a
commit ce04a1a723
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 13 deletions

View File

@ -111,6 +111,13 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
throw new ArgumentNullException(nameof(selectedEncoding)); throw new ArgumentNullException(nameof(selectedEncoding));
} }
// Opt into sync IO support until we can work out an alternative https://github.com/aspnet/AspNetCore/issues/6397
var syncIOFeature = context.HttpContext.Features.Get<Http.Features.IHttpBodyControlFeature>();
if (syncIOFeature != null)
{
syncIOFeature.AllowSynchronousIO = true;
}
var response = context.HttpContext.Response; var response = context.HttpContext.Response;
using (var writer = context.WriterFactory(response.Body, selectedEncoding)) using (var writer = context.WriterFactory(response.Body, selectedEncoding))
{ {

View File

@ -473,17 +473,12 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
MediaTypeHeaderValue contentType, MediaTypeHeaderValue contentType,
MemoryStream responseStream = null) MemoryStream responseStream = null)
{ {
var request = new Mock<HttpRequest>(); var context = new DefaultHttpContext();
var headers = new HeaderDictionary(); context.Request.ContentType = contentType.ToString();
request.Setup(r => r.ContentType).Returns(contentType.ToString()); context.Request.Headers[HeaderNames.AcceptCharset] = contentType.Charset.ToString();
request.SetupGet(r => r.Headers).Returns(headers); context.Response.Body = responseStream ?? new MemoryStream();
headers[HeaderNames.AcceptCharset] = contentType.Charset.ToString();
var response = new Mock<HttpResponse>(); return new ActionContext(context, new RouteData(), new ActionDescriptor());
response.SetupGet(f => f.Body).Returns(responseStream ?? new MemoryStream());
var httpContext = new Mock<HttpContext>();
httpContext.SetupGet(c => c.Request).Returns(request.Object);
httpContext.SetupGet(c => c.Response).Returns(response.Object);
return new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor());
} }
private class TestableJsonOutputFormatter : NewtonsoftJsonOutputFormatter private class TestableJsonOutputFormatter : NewtonsoftJsonOutputFormatter

View File

@ -32,7 +32,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
{ {
Id = 1, Id = 1,
Alias = "john", Alias = "john",
description = "Administrator", description = "This is long so we can test large objects " + new string('a', 1024 * 65),
Designation = "Administrator", Designation = "Administrator",
Name = "John Williams" Name = "John Williams"
}; };

View File

@ -32,7 +32,7 @@ namespace FormatterWebSite.Controllers
{ {
Id = 1, Id = 1,
Alias = "john", Alias = "john",
description = "Administrator", description = "This is long so we can test large objects " + new string('a', 1024 * 65),
Designation = "Administrator", Designation = "Administrator",
Name = "John Williams" Name = "John Williams"
}; };