diff --git a/src/Umbraco.Configuration/AspNetCoreConfigsFactory.cs b/src/Umbraco.Configuration/AspNetCoreConfigsFactory.cs index 1e9f7976d5..0cacab9e1d 100644 --- a/src/Umbraco.Configuration/AspNetCoreConfigsFactory.cs +++ b/src/Umbraco.Configuration/AspNetCoreConfigsFactory.cs @@ -14,7 +14,7 @@ namespace Umbraco.Configuration public AspNetCoreConfigsFactory(IConfiguration configuration) { - _configuration = configuration; + _configuration = configuration ?? throw new System.ArgumentNullException(nameof(configuration)); } public Configs Create() diff --git a/src/Umbraco.Tests.Common/TestHelperBase.cs b/src/Umbraco.Tests.Common/TestHelperBase.cs index ef437d45a6..be43ad6d38 100644 --- a/src/Umbraco.Tests.Common/TestHelperBase.cs +++ b/src/Umbraco.Tests.Common/TestHelperBase.cs @@ -28,15 +28,15 @@ namespace Umbraco.Tests.Common public abstract class TestHelperBase { private readonly ITypeFinder _typeFinder; - private readonly IConfigsFactory _configsFactory; + private IConfigsFactory _configsFactory; private UriUtility _uriUtility; private IIOHelper _ioHelper; private Configs _configs; + private IUmbracoVersion _umbracoVersion; public TestHelperBase(Assembly entryAssembly) { - _configsFactory = new ConfigsFactory(); - SettingsForTests = new SettingsForTests(); + SettingsForTests = new SettingsForTests(); MainDom = new SimpleMainDom(); _typeFinder = new TypeFinder(Mock.Of(), new DefaultUmbracoAssemblyProvider(entryAssembly)); } @@ -54,6 +54,7 @@ namespace Umbraco.Tests.Common _configs = GetConfigsFactory().Create(); return _configs; } + public IRuntimeState GetRuntimeState() { return new RuntimeState( @@ -69,7 +70,12 @@ namespace Umbraco.Tests.Common public abstract IBackOfficeInfo GetBackOfficeInfo(); - public IConfigsFactory GetConfigsFactory() => _configsFactory; + public IConfigsFactory GetConfigsFactory() + { + if (_configsFactory == null) + _configsFactory = new ConfigsFactory(); + return _configsFactory; + } /// /// Gets the current assembly directory. @@ -133,7 +139,9 @@ namespace Umbraco.Tests.Common public IUmbracoVersion GetUmbracoVersion() { - return new UmbracoVersion(GetConfigs().Global()); + if (_umbracoVersion == null) + _umbracoVersion = new UmbracoVersion(GetConfigs().Global()); + return _umbracoVersion; } public IRegister GetRegister() diff --git a/src/Umbraco.Tests.Integration/RuntimeTests.cs b/src/Umbraco.Tests.Integration/RuntimeTests.cs index 034b937a3b..6ccfdd76d6 100644 --- a/src/Umbraco.Tests.Integration/RuntimeTests.cs +++ b/src/Umbraco.Tests.Integration/RuntimeTests.cs @@ -2,7 +2,9 @@ using LightInject.Microsoft.DependencyInjection; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using Moq; using NUnit.Framework; @@ -66,7 +68,13 @@ namespace Umbraco.Tests.Integration var serviceProviderFactory = new UmbracoServiceProviderFactory(container); var umbracoContainer = serviceProviderFactory.GetContainer(); + // Some IConfiguration must exist in the container first + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddEnvironmentVariables(); + services.AddSingleton(x => configurationBuilder.Build()); + // Add it! + services.AddUmbracoConfiguration(); services.AddUmbracoCore(umbracoContainer, GetType().Assembly); // assert results diff --git a/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs b/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs index 579ca104ea..24dcb229c9 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs @@ -33,7 +33,6 @@ namespace Umbraco.Tests.TestHelpers protected override void Compose() { base.Compose(); - base.Compose(); Composition.RegisterUnique(); Composition.RegisterUnique(); diff --git a/src/Umbraco.Web.BackOffice/AspNetCore/UmbracoBackOfficeServiceCollectionExtensions.cs b/src/Umbraco.Web.BackOffice/AspNetCore/UmbracoBackOfficeServiceCollectionExtensions.cs index b795d31670..697e8b7dc4 100644 --- a/src/Umbraco.Web.BackOffice/AspNetCore/UmbracoBackOfficeServiceCollectionExtensions.cs +++ b/src/Umbraco.Web.BackOffice/AspNetCore/UmbracoBackOfficeServiceCollectionExtensions.cs @@ -30,6 +30,9 @@ namespace Umbraco.Web.BackOffice.AspNetCore { var serviceProvider = services.BuildServiceProvider(); var configuration = serviceProvider.GetService(); + if (configuration == null) + throw new InvalidOperationException($"Could not resolve {typeof(IConfiguration)} from the container"); + var configsFactory = new AspNetCoreConfigsFactory(configuration); var configs = configsFactory.Create(); @@ -112,33 +115,6 @@ namespace Umbraco.Web.BackOffice.AspNetCore return coreRuntime; } - public static IServiceCollection CreateCompositionRoot( - this IServiceCollection services, - IHttpContextAccessor httpContextAccessor, - IWebHostEnvironment webHostEnvironment, - IHostApplicationLifetime hostApplicationLifetime, - Configs configs) - { - var hostingSettings = configs.Hosting(); - var coreDebug = configs.CoreDebug(); - var globalSettings = configs.Global(); - - var hostingEnvironment = new AspNetCoreHostingEnvironment(hostingSettings, webHostEnvironment, - httpContextAccessor, hostApplicationLifetime); - var ioHelper = new IOHelper(hostingEnvironment, globalSettings); - var logger = SerilogLogger.CreateWithDefaultConfiguration(hostingEnvironment, - new AspNetCoreSessionIdResolver(httpContextAccessor), - () => services.BuildServiceProvider().GetService(), coreDebug, ioHelper, - new AspNetCoreMarchal()); - - var backOfficeInfo = new AspNetCoreBackOfficeInfo(globalSettings); - var profiler = new LogProfiler(logger); - - Current.Initialize(logger, configs, ioHelper, hostingEnvironment, backOfficeInfo, profiler); - - return services; - } - private static void CreateCompositionRoot(IServiceCollection services) { // TODO: This isn't the best to have to resolve the services now but to avoid this will @@ -150,6 +126,8 @@ namespace Umbraco.Web.BackOffice.AspNetCore var hostApplicationLifetime = serviceProvider.GetRequiredService(); var configs = serviceProvider.GetService(); + if (configs == null) + throw new InvalidOperationException($"Could not resolve type {typeof(Configs)} from the container, ensure {nameof(AddUmbracoConfiguration)} is called before calling {nameof(AddUmbracoCore)}"); var hostingSettings = configs.Hosting(); var coreDebug = configs.CoreDebug();