From 44fd18d78710095d4519c985b8b97bf2c9ffabde Mon Sep 17 00:00:00 2001 From: Shannon Date: Fri, 13 Mar 2020 19:10:21 +1100 Subject: [PATCH] Gets AddUmbracoBackOffice working --- src/Umbraco.Tests.Integration/RuntimeTests.cs | 38 ++++++++++++++++++- ...coBackOfficeServiceCollectionExtensions.cs | 35 ++++++++++------- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/src/Umbraco.Tests.Integration/RuntimeTests.cs b/src/Umbraco.Tests.Integration/RuntimeTests.cs index 32eb327127..828a5b68d1 100644 --- a/src/Umbraco.Tests.Integration/RuntimeTests.cs +++ b/src/Umbraco.Tests.Integration/RuntimeTests.cs @@ -1,4 +1,10 @@ -using Microsoft.Extensions.DependencyInjection; +using LightInject; +using LightInject.Microsoft.DependencyInjection; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Moq; using NUnit.Framework; using System; using System.Linq; @@ -43,6 +49,36 @@ namespace Umbraco.Tests.Integration Assertions.AssertContainer(umbracoContainer.Container, reportOnly: true); // TODO Change that to false eventually when we clean up the container } + [Test] + public void AddUmbracoBackOffice() + { + var testHelper = new TestHelper(); + + // MSDI + var services = new ServiceCollection(); + // These services are required + services.AddSingleton(x => testHelper.GetHttpContextAccessor()); + services.AddSingleton(x => testHelper.GetWebHostEnvironment()); + services.AddSingleton(x => Mock.Of()); + + // LightInject / Umbraco + var container = new ServiceContainer(ContainerOptions.Default.Clone().WithMicrosoftSettings().WithAspNetCoreSettings()); + var serviceProviderFactory = new UmbracoServiceProviderFactory(container); + var umbracoContainer = serviceProviderFactory.GetContainer(); + + // Add it! + services.AddUmbracoCore(umbracoContainer, GetType().Assembly); + + // assert results + var runtimeState = umbracoContainer.GetInstance(); + var mainDom = umbracoContainer.GetInstance(); + + Assert.IsTrue(mainDom.IsMainDom); + Assert.IsNull(runtimeState.BootFailedException); + Assert.AreEqual(RuntimeLevel.Install, runtimeState.Level); + Assert.IsTrue(MyComposer.IsComposed); + } + [RuntimeLevel(MinLevel = RuntimeLevel.Install)] public class MyComposer : IUserComposer { diff --git a/src/Umbraco.Web.BackOffice/AspNetCore/UmbracoBackOfficeServiceCollectionExtensions.cs b/src/Umbraco.Web.BackOffice/AspNetCore/UmbracoBackOfficeServiceCollectionExtensions.cs index 0fbd45214d..6a74323bc4 100644 --- a/src/Umbraco.Web.BackOffice/AspNetCore/UmbracoBackOfficeServiceCollectionExtensions.cs +++ b/src/Umbraco.Web.BackOffice/AspNetCore/UmbracoBackOfficeServiceCollectionExtensions.cs @@ -32,19 +32,28 @@ namespace Umbraco.Web.BackOffice.AspNetCore /// public static IServiceCollection AddUmbracoCore(this IServiceCollection services) { - services.AddSingleton(); - - CreateCompositionRoot(services); - if (!UmbracoServiceProviderFactory.IsActive) throw new InvalidOperationException("Ensure to add UseUmbraco() in your Program.cs after ConfigureWebHostDefaults to enable Umbraco's service provider factory"); var umbContainer = UmbracoServiceProviderFactory.UmbracoContainer; + return services.AddUmbracoCore(umbContainer, Assembly.GetEntryAssembly()); + } + + public static IServiceCollection AddUmbracoCore(this IServiceCollection services, IRegister umbContainer, Assembly entryAssembly) + { + services.AddSingleton(); + + CreateCompositionRoot(services); + // TODO: Get rid of this 'Current' requirement var globalSettings = Current.Configs.Global(); var umbracoVersion = new UmbracoVersion(globalSettings); + // TODO: Currently we are not passing in any TypeFinderConfig (with ITypeFinderSettings) which we should do, however + // this is not critical right now and would require loading in some config before boot time so just leaving this as-is for now. + var typeFinder = new TypeFinder(Current.Logger, new DefaultUmbracoAssemblyProvider(entryAssembly)); + var coreRuntime = GetCoreRuntime( Current.Configs, umbracoVersion, @@ -52,7 +61,8 @@ namespace Umbraco.Web.BackOffice.AspNetCore Current.Logger, Current.Profiler, Current.HostingEnvironment, - Current.BackOfficeInfo); + Current.BackOfficeInfo, + typeFinder); var factory = coreRuntime.Boot(umbContainer); @@ -60,7 +70,8 @@ namespace Umbraco.Web.BackOffice.AspNetCore } private static IRuntime GetCoreRuntime(Configs configs, IUmbracoVersion umbracoVersion, IIOHelper ioHelper, ILogger logger, - IProfiler profiler, Core.Hosting.IHostingEnvironment hostingEnvironment, IBackOfficeInfo backOfficeInfo) + IProfiler profiler, Core.Hosting.IHostingEnvironment hostingEnvironment, IBackOfficeInfo backOfficeInfo, + ITypeFinder typeFinder) { var connectionStringConfig = configs.ConnectionStrings()[Constants.System.UmbracoConnectionName]; var dbProviderFactoryCreator = new SqlServerDbProviderFactoryCreator( @@ -75,10 +86,6 @@ namespace Umbraco.Web.BackOffice.AspNetCore var mainDom = new MainDom(logger, hostingEnvironment, mainDomLock); - // TODO: Currently we are not passing in any TypeFinderConfig (with ITypeFinderSettings) which we should do, however - // this is not critical right now and would require loading in some config before boot time so just leaving this as-is for now. - var typeFinder = new TypeFinder(logger, new DefaultUmbracoAssemblyProvider(Assembly.GetEntryAssembly())); - var coreRuntime = new CoreRuntime(configs, umbracoVersion, ioHelper, logger, profiler, new AspNetCoreBootPermissionsChecker(), hostingEnvironment, backOfficeInfo, dbProviderFactoryCreator, mainDom, typeFinder); @@ -87,11 +94,13 @@ namespace Umbraco.Web.BackOffice.AspNetCore private static void CreateCompositionRoot(IServiceCollection services) { + // TODO: This isn't the best to have to resolve the services now but to avoid this will + // require quite a lot of re-work. var serviceProvider = services.BuildServiceProvider(); - var httpContextAccessor = serviceProvider.GetService(); - var webHostEnvironment = serviceProvider.GetService(); - var hostApplicationLifetime = serviceProvider.GetService(); + var httpContextAccessor = serviceProvider.GetRequiredService(); + var webHostEnvironment = serviceProvider.GetRequiredService(); + var hostApplicationLifetime = serviceProvider.GetRequiredService(); var configFactory = new ConfigsFactory();