diff --git a/src/Umbraco.Abstractions/Composing/Current.cs b/src/Umbraco.Abstractions/Composing/Current.cs index c72f87dfab..41e1ea2c21 100644 --- a/src/Umbraco.Abstractions/Composing/Current.cs +++ b/src/Umbraco.Abstractions/Composing/Current.cs @@ -10,25 +10,27 @@ namespace Umbraco.Composing { public static class Current { - private static bool _initialized; - private static ILogger _logger = new NullLogger(); private static Configs _configs; private static IIOHelper _ioHelper; private static IHostingEnvironment _hostingEnvironment; private static IBackOfficeInfo _backOfficeInfo; + private static IProfiler _profiler; public static ILogger Logger => EnsureInitialized(_logger); public static Configs Configs => EnsureInitialized(_configs); public static IIOHelper IOHelper => EnsureInitialized(_ioHelper); public static IHostingEnvironment HostingEnvironment => EnsureInitialized(_hostingEnvironment); public static IBackOfficeInfo BackOfficeInfo => EnsureInitialized(_backOfficeInfo); + public static IProfiler Profiler => EnsureInitialized(_profiler); + + public static bool IsInitialized { get; private set; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static T EnsureInitialized(T returnValue) where T : class { - if (returnValue is null && !_initialized) + if (returnValue is null && !IsInitialized) throw new InvalidOperationException("Current cannot be used before initialize"); return returnValue; } @@ -38,9 +40,10 @@ namespace Umbraco.Composing Configs configs, IIOHelper ioHelper, IHostingEnvironment hostingEnvironment, - IBackOfficeInfo backOfficeInfo) + IBackOfficeInfo backOfficeInfo, + IProfiler profiler) { - if (_initialized) + if (IsInitialized) { throw new InvalidOperationException("Current cannot be initialized more than once"); } @@ -50,8 +53,9 @@ namespace Umbraco.Composing _ioHelper = ioHelper ?? throw new ArgumentNullException(nameof(ioHelper)); _hostingEnvironment = hostingEnvironment ?? throw new ArgumentNullException(nameof(hostingEnvironment)); _backOfficeInfo = backOfficeInfo ?? throw new ArgumentNullException(nameof(backOfficeInfo)); + _profiler = profiler ?? throw new ArgumentNullException(nameof(profiler)); - _initialized = true; + IsInitialized = true; } } } diff --git a/src/Umbraco.Web/UmbracoApplicationBase.cs b/src/Umbraco.Web/UmbracoApplicationBase.cs index 9ae11c9983..12925604db 100644 --- a/src/Umbraco.Web/UmbracoApplicationBase.cs +++ b/src/Umbraco.Web/UmbracoApplicationBase.cs @@ -23,43 +23,36 @@ namespace Umbraco.Web { private IRuntime _runtime; - private readonly ILogger _logger; - private readonly Configs _configs; - private readonly IIOHelper _ioHelper; - private readonly IProfiler _profiler; - private readonly IHostingEnvironment _hostingEnvironment; - private readonly IBackOfficeInfo _backOfficeInfo; private IFactory _factory; protected UmbracoApplicationBase() { - var configFactory = new ConfigsFactory(); - var hostingSettings = configFactory.HostingSettings; - var coreDebug = configFactory.CoreDebug; + if (!Umbraco.Composing.Current.IsInitialized) + { + var configFactory = new ConfigsFactory(); - _hostingEnvironment = new AspNetHostingEnvironment(hostingSettings); - _ioHelper = new IOHelper(_hostingEnvironment); - _configs = configFactory.Create(_ioHelper); + var hostingSettings = configFactory.HostingSettings; + var coreDebug = configFactory.CoreDebug; - _profiler = new LogProfiler(_logger); + var hostingEnvironment = new AspNetHostingEnvironment(hostingSettings); + var ioHelper = new IOHelper(hostingEnvironment); + var configs = configFactory.Create(ioHelper); - _logger = SerilogLogger.CreateWithDefaultConfiguration(_hostingEnvironment, new AspNetSessionIdResolver(), () => _factory?.GetInstance(), coreDebug, _ioHelper, new FrameworkMarchal()); - _backOfficeInfo = new AspNetBackOfficeInfo(_configs.Global(), _ioHelper, _configs.Settings(), _logger); + var logger = SerilogLogger.CreateWithDefaultConfiguration(hostingEnvironment, new AspNetSessionIdResolver(), () => _factory?.GetInstance(), coreDebug, ioHelper, new FrameworkMarchal()); + var backOfficeInfo = new AspNetBackOfficeInfo(configs.Global(), ioHelper, configs.Settings(), logger); + var profiler = new LogProfiler(logger); + Umbraco.Composing.Current.Initialize(logger, configs, ioHelper, hostingEnvironment, backOfficeInfo, profiler); + } - Umbraco.Composing.Current.Initialize(_logger, _configs, _ioHelper, _hostingEnvironment, _backOfficeInfo); } protected UmbracoApplicationBase(ILogger logger, Configs configs, IIOHelper ioHelper, IProfiler profiler, IHostingEnvironment hostingEnvironment, IBackOfficeInfo backOfficeInfo) { - _logger = logger; - _configs = configs; - _ioHelper = ioHelper; - _profiler = profiler; - _hostingEnvironment = hostingEnvironment; - _backOfficeInfo = backOfficeInfo; - - Umbraco.Composing.Current.Initialize(_logger, _configs, _ioHelper, _hostingEnvironment, _backOfficeInfo); + if (!Umbraco.Composing.Current.IsInitialized) + { + Umbraco.Composing.Current.Initialize(logger, configs, ioHelper, hostingEnvironment, backOfficeInfo, profiler); + } } @@ -105,13 +98,20 @@ namespace Umbraco.Web // ******** THIS IS WHERE EVERYTHING BEGINS ******** - var globalSettings = _configs.Global(); + var globalSettings = Umbraco.Composing.Current.Configs.Global(); var umbracoVersion = new UmbracoVersion(globalSettings); // create the register for the application, and boot // the boot manager is responsible for registrations var register = GetRegister(globalSettings); - _runtime = GetRuntime(_configs, umbracoVersion, _ioHelper, _logger, _profiler, _hostingEnvironment, _backOfficeInfo); + _runtime = GetRuntime( + Umbraco.Composing.Current.Configs, + umbracoVersion, + Umbraco.Composing.Current.IOHelper, + Umbraco.Composing.Current.Logger, + Umbraco.Composing.Current.Profiler, + Umbraco.Composing.Current.HostingEnvironment, + Umbraco.Composing.Current.BackOfficeInfo); _factory =_runtime.Boot(register); }