From ce04a1a7235f76059c8f2cffa2ad5c960cceab06 Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Thu, 7 Mar 2019 19:11:35 -0800 Subject: [PATCH] Allow sync IO for NewtonsoftJsonOutputFormatter #8302 (#8303) --- .../src/NewtonsoftJsonOutputFormatter.cs | 7 +++++++ .../test/NewtonsoftJsonOutputFormatterTest.cs | 17 ++++++----------- .../JsonOutputFormatterTests.cs | 2 +- .../Controllers/JsonFormatterController.cs | 2 +- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonOutputFormatter.cs b/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonOutputFormatter.cs index 382ca577ff..6e71a1c00b 100644 --- a/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonOutputFormatter.cs +++ b/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonOutputFormatter.cs @@ -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(); + if (syncIOFeature != null) + { + syncIOFeature.AllowSynchronousIO = true; + } + var response = context.HttpContext.Response; using (var writer = context.WriterFactory(response.Body, selectedEncoding)) { diff --git a/src/Mvc/Mvc.NewtonsoftJson/test/NewtonsoftJsonOutputFormatterTest.cs b/src/Mvc/Mvc.NewtonsoftJson/test/NewtonsoftJsonOutputFormatterTest.cs index 8b3b246261..1fff43320e 100644 --- a/src/Mvc/Mvc.NewtonsoftJson/test/NewtonsoftJsonOutputFormatterTest.cs +++ b/src/Mvc/Mvc.NewtonsoftJson/test/NewtonsoftJsonOutputFormatterTest.cs @@ -473,17 +473,12 @@ namespace Microsoft.AspNetCore.Mvc.Formatters MediaTypeHeaderValue contentType, MemoryStream responseStream = null) { - var request = new Mock(); - 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(); - response.SetupGet(f => f.Body).Returns(responseStream ?? new MemoryStream()); - var httpContext = new Mock(); - 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 diff --git a/src/Mvc/test/Mvc.FunctionalTests/JsonOutputFormatterTests.cs b/src/Mvc/test/Mvc.FunctionalTests/JsonOutputFormatterTests.cs index 692438f82b..2d764da29a 100644 --- a/src/Mvc/test/Mvc.FunctionalTests/JsonOutputFormatterTests.cs +++ b/src/Mvc/test/Mvc.FunctionalTests/JsonOutputFormatterTests.cs @@ -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" }; diff --git a/src/Mvc/test/WebSites/FormatterWebSite/Controllers/JsonFormatterController.cs b/src/Mvc/test/WebSites/FormatterWebSite/Controllers/JsonFormatterController.cs index 569b86cc24..48e29ca041 100644 --- a/src/Mvc/test/WebSites/FormatterWebSite/Controllers/JsonFormatterController.cs +++ b/src/Mvc/test/WebSites/FormatterWebSite/Controllers/JsonFormatterController.cs @@ -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" };