From 88f6d09ef937c094208e10f97f0989631a3764d1 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 14 May 2020 22:14:00 +1000 Subject: [PATCH] Enables the boot failed exception logic --- .../ApplicationBuilderExtensions.cs | 2 ++ .../UmbracoWebServiceCollectionExtensions.cs | 4 --- .../Middleware/BootFailedMiddleware.cs | 34 +++++++++++++++++++ .../Runtime/AspNetCoreComposer.cs | 8 +++++ src/Umbraco.Web/UmbracoInjectedModule.cs | 19 ----------- 5 files changed, 44 insertions(+), 23 deletions(-) create mode 100644 src/Umbraco.Web.Common/Middleware/BootFailedMiddleware.cs diff --git a/src/Umbraco.Web.Common/Extensions/ApplicationBuilderExtensions.cs b/src/Umbraco.Web.Common/Extensions/ApplicationBuilderExtensions.cs index 228f133ab0..a43094a8f9 100644 --- a/src/Umbraco.Web.Common/Extensions/ApplicationBuilderExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/ApplicationBuilderExtensions.cs @@ -73,6 +73,8 @@ namespace Umbraco.Extensions { if (app == null) throw new ArgumentNullException(nameof(app)); + app.UseMiddleware(); + if (!app.UmbracoCanBoot()) return app; app.UseMiddleware(); diff --git a/src/Umbraco.Web.Common/Extensions/UmbracoWebServiceCollectionExtensions.cs b/src/Umbraco.Web.Common/Extensions/UmbracoWebServiceCollectionExtensions.cs index 559730bdc3..a3e9b901dc 100644 --- a/src/Umbraco.Web.Common/Extensions/UmbracoWebServiceCollectionExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/UmbracoWebServiceCollectionExtensions.cs @@ -34,10 +34,6 @@ namespace Umbraco.Extensions /// public static IServiceCollection AddUmbracoWebComponents(this IServiceCollection services) { - services.AddTransient(); - services.AddTransient(); - - services.TryAddSingleton(); services.ConfigureOptions(); services.TryAddEnumerable(ServiceDescriptor.Transient()); diff --git a/src/Umbraco.Web.Common/Middleware/BootFailedMiddleware.cs b/src/Umbraco.Web.Common/Middleware/BootFailedMiddleware.cs new file mode 100644 index 0000000000..685312778d --- /dev/null +++ b/src/Umbraco.Web.Common/Middleware/BootFailedMiddleware.cs @@ -0,0 +1,34 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Umbraco.Core; +using Umbraco.Core.Exceptions; + +namespace Umbraco.Web.Common.Middleware +{ + /// + /// Executes when Umbraco booting fails in order to show the problem + /// + public class BootFailedMiddleware : IMiddleware + { + private readonly IRuntimeState _runtimeState; + + public BootFailedMiddleware(IRuntimeState runtimeState) + { + _runtimeState = runtimeState; + } + + public async Task InvokeAsync(HttpContext context, RequestDelegate next) + { + if (_runtimeState.Level == RuntimeLevel.BootFailed) + { + // short circuit + BootFailedException.Rethrow(_runtimeState.BootFailedException); + } + else + { + await next(context); + } + } + } +} diff --git a/src/Umbraco.Web.Common/Runtime/AspNetCoreComposer.cs b/src/Umbraco.Web.Common/Runtime/AspNetCoreComposer.cs index c2a29c63a6..a2e6ba2a02 100644 --- a/src/Umbraco.Web.Common/Runtime/AspNetCoreComposer.cs +++ b/src/Umbraco.Web.Common/Runtime/AspNetCoreComposer.cs @@ -18,6 +18,9 @@ using Umbraco.Web.Common.Install; using Umbraco.Extensions; using System.Linq; using Umbraco.Web.Common.Controllers; +using System; +using Umbraco.Web.Common.Middleware; +using Umbraco.Web.Common.ModelBinding; namespace Umbraco.Web.Common.Runtime { @@ -77,6 +80,11 @@ namespace Umbraco.Web.Common.Runtime composition.RegisterUnique(); + composition.RegisterUnique(); + composition.RegisterUnique(); + composition.RegisterUnique(); + + composition.RegisterUnique(); } } } diff --git a/src/Umbraco.Web/UmbracoInjectedModule.cs b/src/Umbraco.Web/UmbracoInjectedModule.cs index 1df99ad45f..95adb9c64e 100644 --- a/src/Umbraco.Web/UmbracoInjectedModule.cs +++ b/src/Umbraco.Web/UmbracoInjectedModule.cs @@ -361,25 +361,6 @@ namespace Umbraco.Web /// public void Init(HttpApplication app) { - if (_runtime.Level == RuntimeLevel.BootFailed) - { - // there's nothing we can do really - app.BeginRequest += (sender, args) => - { - // would love to avoid throwing, and instead display a customized Umbraco 500 - // page - however if we don't throw here, something else might go wrong, and - // it's this later exception that would be reported. could not figure out how - // to prevent it, either with httpContext.Response.End() or .ApplicationInstance - // .CompleteRequest() - - // also, if something goes wrong with our DI setup, the logging subsystem may - // not even kick in, so here we try to give as much detail as possible - - BootFailedException.Rethrow(Current.RuntimeState.BootFailedException); - }; - return; - } - app.BeginRequest += (sender, e) => { var httpContext = ((HttpApplication) sender).Context;