2020-02-18 08:32:06 +01:00
using System ;
using Microsoft.AspNetCore.Builder ;
2020-03-24 14:48:32 +11:00
using Microsoft.Extensions.DependencyInjection ;
2020-04-22 14:23:56 +10:00
using Serilog.Context ;
2020-04-03 15:56:34 +11:00
using Smidge ;
2020-03-25 15:06:22 +11:00
using Umbraco.Core ;
2020-04-22 15:23:51 +10:00
using Umbraco.Core.Configuration ;
2020-03-25 15:06:22 +11:00
using Umbraco.Core.Hosting ;
2020-05-08 17:36:59 +10:00
using Umbraco.Extensions ;
2020-04-22 15:23:51 +10:00
using Umbraco.Infrastructure.Logging.Serilog.Enrichers ;
2020-04-22 14:23:56 +10:00
using Umbraco.Web.Common.Middleware ;
2020-02-18 08:32:06 +01:00
namespace Umbraco.Web.BackOffice.AspNetCore
{
2020-04-03 15:56:34 +11:00
public static class UmbracoApplicationBuilderExtensions
2020-02-18 08:32:06 +01:00
{
2020-02-24 11:26:34 +01:00
public static IApplicationBuilder UseUmbracoBackOffice ( this IApplicationBuilder app )
2020-02-18 08:32:06 +01:00
{
2020-03-24 14:48:32 +11:00
if ( app = = null ) throw new ArgumentNullException ( nameof ( app ) ) ;
2020-02-18 08:32:06 +01:00
2020-05-08 17:36:59 +10:00
if ( ! app . UmbracoCanBoot ( ) ) return app ;
2020-05-08 17:30:30 +10:00
// TODO: start the back office
2020-02-24 11:26:34 +01:00
return app ;
2020-02-18 08:32:06 +01:00
}
2020-03-24 14:48:32 +11:00
2020-03-25 15:06:22 +11:00
/// <summary>
/// Start Umbraco
/// </summary>
/// <param name="app"></param>
/// <returns></returns>
public static IApplicationBuilder UseUmbracoCore ( this IApplicationBuilder app )
{
if ( app = = null ) throw new ArgumentNullException ( nameof ( app ) ) ;
2020-05-08 17:36:59 +10:00
if ( app . UmbracoCanBoot ( ) )
2020-05-08 17:30:30 +10:00
{
2020-05-08 17:36:59 +10:00
var runtime = app . ApplicationServices . GetRequiredService < IRuntime > ( ) ;
2020-05-08 17:30:30 +10:00
// Register a listener for application shutdown in order to terminate the runtime
var hostLifetime = app . ApplicationServices . GetRequiredService < IApplicationShutdownRegistry > ( ) ;
var runtimeShutdown = new CoreRuntimeShutdown ( runtime , hostLifetime ) ;
hostLifetime . RegisterObject ( runtimeShutdown ) ;
// Register our global threadabort enricher for logging
var threadAbortEnricher = app . ApplicationServices . GetRequiredService < ThreadAbortExceptionEnricher > ( ) ;
LogContext . Push ( threadAbortEnricher ) ; // NOTE: We are not in a using clause because we are not removing it, it is on the global context
2020-04-22 15:23:51 +10:00
2020-05-08 17:30:30 +10:00
// Start the runtime!
runtime . Start ( ) ;
}
else
{
2020-05-08 17:36:59 +10:00
// TODO: Register simple middleware to show the error like we used to in UmbracoModule? Or maybe that's part of a UseUmbracoWebsite/backoffice type thing .. probably :)
2020-05-08 17:30:30 +10:00
}
2020-03-25 15:06:22 +11:00
return app ;
}
/// <summary>
/// Ensures the runtime is shutdown when the application is shutting down
/// </summary>
private class CoreRuntimeShutdown : IRegisteredObject
{
2020-03-26 15:39:20 +11:00
public CoreRuntimeShutdown ( IRuntime runtime , IApplicationShutdownRegistry hostLifetime )
2020-03-25 15:06:22 +11:00
{
_runtime = runtime ;
_hostLifetime = hostLifetime ;
}
private bool _completed = false ;
private readonly IRuntime _runtime ;
2020-03-26 15:39:20 +11:00
private readonly IApplicationShutdownRegistry _hostLifetime ;
2020-03-25 15:06:22 +11:00
public void Stop ( bool immediate )
{
if ( ! _completed )
{
_completed = true ;
_runtime . Terminate ( ) ;
_hostLifetime . UnregisterObject ( this ) ;
}
}
}
2020-04-03 15:56:34 +11:00
2020-04-22 14:23:56 +10:00
public static IApplicationBuilder UseUmbracoRequestLogging ( this IApplicationBuilder app )
{
if ( app = = null ) throw new ArgumentNullException ( nameof ( app ) ) ;
2020-05-08 17:36:59 +10:00
if ( ! app . UmbracoCanBoot ( ) ) return app ;
2020-05-08 17:30:30 +10:00
2020-04-22 14:23:56 +10:00
app . UseMiddleware < UmbracoRequestLoggingMiddleware > ( ) ;
return app ;
}
2020-04-03 15:56:34 +11:00
public static IApplicationBuilder UseUmbracoRuntimeMinification ( this IApplicationBuilder app )
{
if ( app = = null ) throw new ArgumentNullException ( nameof ( app ) ) ;
2020-05-08 17:36:59 +10:00
if ( ! app . UmbracoCanBoot ( ) ) return app ;
2020-05-08 17:30:30 +10:00
2020-04-03 15:56:34 +11:00
app . UseSmidge ( ) ;
return app ;
}
2020-02-18 08:32:06 +01:00
}
}