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));
}
// 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;
using (var writer = context.WriterFactory(response.Body, selectedEncoding))
{

View File

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

View File

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

View File

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