From b1f6fb914f6029fc96d754d8c3357fed3b51ce27 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Fri, 28 Aug 2020 11:54:36 +0200 Subject: [PATCH 1/2] Resolves issue with rendering back-office media selections when synchronous IO is not allowed. --- .../DetermineAmbiguousActionByPassingParameters.cs | 13 +++++++++---- .../UmbracoBackOfficeServiceCollectionExtensions.cs | 1 - .../Extensions/HttpRequestExtensions.cs | 2 -- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Umbraco.Web.BackOffice/Controllers/DetermineAmbiguousActionByPassingParameters.cs b/src/Umbraco.Web.BackOffice/Controllers/DetermineAmbiguousActionByPassingParameters.cs index a6432402b4..fc6c3b6814 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/DetermineAmbiguousActionByPassingParameters.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/DetermineAmbiguousActionByPassingParameters.cs @@ -3,15 +3,16 @@ using System.Collections; using System.Collections.Generic; using System.Linq; using System.Net.Http; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.ActionConstraints; using Microsoft.AspNetCore.Routing; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using Umbraco.Core; using Umbraco.Extensions; using Umbraco.Web.BackOffice.ModelBinders; -using Microsoft.AspNetCore.Http; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; namespace Umbraco.Web.BackOffice.Controllers { @@ -102,7 +103,11 @@ namespace Umbraco.Web.BackOffice.Controllers // IMPORTANT: Ensure the requestBody can be read multiple times. routeContext.HttpContext.Request.EnableBuffering(); - var body = _requestBody ??= routeContext.HttpContext.Request.GetRawBodyString(); + // We need to use the asynchronous method here if synchronous IO is not allowed (it may or may not be, depending + // on configuration in UmbracoBackOfficeServiceCollectionExtensions.AddUmbraco()). + // We can't use async/await due to the need to override IsValidForRequest, which doesn't have an async override, so going with + // this, which seems to be the least worst option for "sync to async" (https://stackoverflow.com/a/32429753/489433). + var body = _requestBody ??= Task.Run(() => routeContext.HttpContext.Request.GetRawBodyStringAsync()).GetAwaiter().GetResult(); var jToken = JsonConvert.DeserializeObject(body); diff --git a/src/Umbraco.Web.BackOffice/Extensions/UmbracoBackOfficeServiceCollectionExtensions.cs b/src/Umbraco.Web.BackOffice/Extensions/UmbracoBackOfficeServiceCollectionExtensions.cs index fc3efab5e0..7bdc8b1b5f 100644 --- a/src/Umbraco.Web.BackOffice/Extensions/UmbracoBackOfficeServiceCollectionExtensions.cs +++ b/src/Umbraco.Web.BackOffice/Extensions/UmbracoBackOfficeServiceCollectionExtensions.cs @@ -50,7 +50,6 @@ namespace Umbraco.Extensions //But as far as I can see, there are still precompiled views, even when this is activated, so maybe it is okay. services.AddControllersWithViews().AddRazorRuntimeCompilation(); - // If using Kestrel: https://stackoverflow.com/a/55196057 services.Configure(options => { diff --git a/src/Umbraco.Web.Common/Extensions/HttpRequestExtensions.cs b/src/Umbraco.Web.Common/Extensions/HttpRequestExtensions.cs index 5baedf3ded..ff481b7d37 100644 --- a/src/Umbraco.Web.Common/Extensions/HttpRequestExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/HttpRequestExtensions.cs @@ -78,8 +78,6 @@ namespace Umbraco.Extensions request.Body.Seek(0, SeekOrigin.Begin); return result; } - - } public static async Task GetRawBodyStringAsync(this HttpRequest request, Encoding encoding = null) From bdfa1fb644e5bdfafb3961bece3ff7189026694a Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Fri, 28 Aug 2020 12:56:40 +0200 Subject: [PATCH 2/2] Removed the server option to allow synchronous IO. --- .../UmbracoBackOfficeServiceCollectionExtensions.cs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/Umbraco.Web.BackOffice/Extensions/UmbracoBackOfficeServiceCollectionExtensions.cs b/src/Umbraco.Web.BackOffice/Extensions/UmbracoBackOfficeServiceCollectionExtensions.cs index 7bdc8b1b5f..e09a29d157 100644 --- a/src/Umbraco.Web.BackOffice/Extensions/UmbracoBackOfficeServiceCollectionExtensions.cs +++ b/src/Umbraco.Web.BackOffice/Extensions/UmbracoBackOfficeServiceCollectionExtensions.cs @@ -50,17 +50,6 @@ namespace Umbraco.Extensions //But as far as I can see, there are still precompiled views, even when this is activated, so maybe it is okay. services.AddControllersWithViews().AddRazorRuntimeCompilation(); - // If using Kestrel: https://stackoverflow.com/a/55196057 - services.Configure(options => - { - options.AllowSynchronousIO = true; - }); - - services.Configure(options => - { - options.AllowSynchronousIO = true; - }); - return services; }