diff --git a/src/Umbraco.Infrastructure/Composing/HostBuilderExtensions.cs b/src/Umbraco.Infrastructure/Composing/HostBuilderExtensions.cs index e2be3569d6..8e1176747c 100644 --- a/src/Umbraco.Infrastructure/Composing/HostBuilderExtensions.cs +++ b/src/Umbraco.Infrastructure/Composing/HostBuilderExtensions.cs @@ -12,12 +12,7 @@ namespace Umbraco.Core.Composing /// Adds CoreRuntime as HostedService /// /// - /// When running the site should be called before ConfigureWebDefaults. - /// - /// - /// When testing should be called after ConfigureWebDefaults to ensure UseTestDatabase is called before CoreRuntime - /// starts or we initialize components with incorrect run level. - /// + /// Should be called before ConfigureWebDefaults. /// public static IHostBuilder UseUmbraco(this IHostBuilder builder) { diff --git a/src/Umbraco.Tests.Integration/RuntimeTests.cs b/src/Umbraco.Tests.Integration/RuntimeTests.cs index 5d721ad176..cd6909a8c5 100644 --- a/src/Umbraco.Tests.Integration/RuntimeTests.cs +++ b/src/Umbraco.Tests.Integration/RuntimeTests.cs @@ -43,6 +43,7 @@ namespace Umbraco.Tests.Integration var testHelper = new TestHelper(); var hostBuilder = new HostBuilder() + .UseUmbraco() .ConfigureServices((hostContext, services) => { var webHostEnvironment = testHelper.GetWebHostEnvironment(); @@ -68,8 +69,7 @@ namespace Umbraco.Tests.Integration .Build(); services.AddRouting(); // LinkGenerator - }) - .UseUmbraco(); + }); var host = await hostBuilder.StartAsync(); var app = new ApplicationBuilder(host.Services); diff --git a/src/Umbraco.Tests.Integration/TestServerTest/UmbracoTestServerTestBase.cs b/src/Umbraco.Tests.Integration/TestServerTest/UmbracoTestServerTestBase.cs index 8e6c02ca15..2b7aea8189 100644 --- a/src/Umbraco.Tests.Integration/TestServerTest/UmbracoTestServerTestBase.cs +++ b/src/Umbraco.Tests.Integration/TestServerTest/UmbracoTestServerTestBase.cs @@ -19,7 +19,6 @@ using Umbraco.Core.DependencyInjection; using Umbraco.Web.Common.Controllers; using Microsoft.Extensions.Hosting; using Umbraco.Core.Cache; -using Umbraco.Core.Composing; using Umbraco.Web.BackOffice.Controllers; namespace Umbraco.Tests.Integration.TestServerTest @@ -35,7 +34,7 @@ namespace Umbraco.Tests.Integration.TestServerTest InMemoryConfiguration["Umbraco:CMS:Hosting:Debug"] = "true"; // create new WebApplicationFactory specifying 'this' as the IStartup instance - var factory = new UmbracoWebApplicationFactory(CreateHostBuilder); + var factory = new UmbracoWebApplicationFactory(CreateHostBuilder, BeforeHostStart); // additional host configuration for web server integration tests Factory = factory.WithWebHostBuilder(builder => @@ -71,15 +70,11 @@ namespace Umbraco.Tests.Integration.TestServerTest // call startup builder.Configure(app => { - Services = app.ApplicationServices; - UseTestDatabase(app.ApplicationServices); Configure(app); }); }).UseEnvironment(Environments.Development); - builder.UseUmbraco(); // Ensures CoreRuntime.StartAsync is called, must be after ConfigureWebHost - return builder; } diff --git a/src/Umbraco.Tests.Integration/TestServerTest/UmbracoWebApplicationFactory.cs b/src/Umbraco.Tests.Integration/TestServerTest/UmbracoWebApplicationFactory.cs index 251f155d2c..256c5c59a9 100644 --- a/src/Umbraco.Tests.Integration/TestServerTest/UmbracoWebApplicationFactory.cs +++ b/src/Umbraco.Tests.Integration/TestServerTest/UmbracoWebApplicationFactory.cs @@ -1,4 +1,4 @@ -using System; +using System; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.Hosting; @@ -8,16 +8,30 @@ namespace Umbraco.Tests.Integration.TestServerTest public class UmbracoWebApplicationFactory : WebApplicationFactory where TStartup : class { private readonly Func _createHostBuilder; + private readonly Action _beforeStart; /// /// Constructor to create a new WebApplicationFactory /// /// Method to create the IHostBuilder - public UmbracoWebApplicationFactory(Func createHostBuilder) + /// Method to perform an action before IHost starts + public UmbracoWebApplicationFactory(Func createHostBuilder, Action beforeStart = null) { _createHostBuilder = createHostBuilder; + _beforeStart = beforeStart; } protected override IHostBuilder CreateHostBuilder() => _createHostBuilder(); + + protected override IHost CreateHost(IHostBuilder builder) + { + IHost host = builder.Build(); + + _beforeStart?.Invoke(host); + + host.Start(); + + return host; + } } } diff --git a/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs b/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs index 52b74f6bbb..e62b3f5c7f 100644 --- a/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs +++ b/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs @@ -97,18 +97,22 @@ namespace Umbraco.Tests.Integration.Testing public virtual void Setup() { InMemoryConfiguration[Constants.Configuration.ConfigGlobal + ":" + nameof(GlobalSettings.InstallEmptyDatabase)] = "true"; - var hostBuilder = CreateHostBuilder() - .UseUmbraco(); // This ensures CoreRuntime.StartAsync will be called (however it's a mock if boot = false) + var hostBuilder = CreateHostBuilder(); IHost host = hostBuilder.Build(); - Services = host.Services; + BeforeHostStart(host); host.Start(); var app = new ApplicationBuilder(host.Services); - Configure(app); } + protected void BeforeHostStart(IHost host) + { + Services = host.Services; + UseTestDatabase(Services); + } + #region Generic Host Builder and Runtime private ILoggerFactory CreateLoggerFactory() @@ -149,12 +153,12 @@ namespace Umbraco.Tests.Integration.Testing public virtual IHostBuilder CreateHostBuilder() { var hostBuilder = Host.CreateDefaultBuilder() + .UseUmbraco() // IMPORTANT: We Cannot use UseStartup, there's all sorts of threads about this with testing. Although this can work // if you want to setup your tests this way, it is a bit annoying to do that as the WebApplicationFactory will // create separate Host instances. So instead of UseStartup, we just call ConfigureServices/Configure ourselves, // and in the case of the UmbracoTestServerTestBase it will use the ConfigureWebHost to Configure the IApplicationBuilder directly. //.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(GetType()); }) - .ConfigureAppConfiguration((context, configBuilder) => { context.HostingEnvironment = TestHelper.GetWebHostEnvironment(); @@ -233,8 +237,6 @@ namespace Umbraco.Tests.Integration.Testing public virtual void Configure(IApplicationBuilder app) { - UseTestDatabase(app.ApplicationServices); - if (TestOptions.Boot) { Services.GetRequiredService().EnsureBackOfficeSecurity();