diff --git a/src/Umbraco.Core/Composing/ContainerFactory.cs b/src/Umbraco.Core/Composing/ContainerFactory.cs index 5c85072298..fb2c5f6eb7 100644 --- a/src/Umbraco.Core/Composing/ContainerFactory.cs +++ b/src/Umbraco.Core/Composing/ContainerFactory.cs @@ -1,36 +1,54 @@ using System; using System.Configuration; using System.Reflection; -using Umbraco.Core.Composing.LightInject; namespace Umbraco.Core.Composing { - public class ContainerFactory + /// + /// Creates the container. + /// + public static class ContainerFactory { + // cannot use typeof().AssemblyQualifiedName on the web container - we don't reference it + // a normal Umbraco site should run on the web container, but an app may run on the core one + private const string CoreLightInjectContainerTypeName = "Umbraco.Core.Composing.LightInject.LightInjectContainer,Umbraco.Core"; + private const string WebLightInjectContainerTypeName = "Umbraco.Web.Composing.LightInject.LightInjectContainer,Umbraco.Web"; + /// /// Creates a new instance of the configured container. + /// + /// /// To override the default LightInjectContainer, add an appSetting named umbracoContainerType with /// a fully qualified type name to a class with a static method "Create" returning an IContainer. - /// + /// public static IContainer Create() { - var configuredTypeName = ConfigurationManager.AppSettings["umbracoContainerType"] - ?? typeof(LightInjectContainer).AssemblyQualifiedName; - var type = Type.GetType(configuredTypeName); + Type type; + + var configuredTypeName = ConfigurationManager.AppSettings["umbracoContainerType"]; + if (configuredTypeName.IsNullOrWhiteSpace()) + { + // try to get the web LightInject container type, + // else the core LightInject container type + type = Type.GetType(configuredTypeName = WebLightInjectContainerTypeName) ?? + Type.GetType(configuredTypeName = CoreLightInjectContainerTypeName); + } + else + { + // try to get the configured container type + type = Type.GetType(configuredTypeName); + } + if (type == null) - { - throw new Exception($"Cannot find container factory class named '${configuredTypeName}'"); - } - var factoryMethod = type.GetMethod("Create", BindingFlags.Static); + throw new Exception($"Cannot find container factory class '{configuredTypeName}'."); + + var factoryMethod = type.GetMethod("Create", BindingFlags.Public | BindingFlags.Static); if (factoryMethod == null) - { - throw new Exception($"Container factory class '${configuredTypeName}' does not have a public static method named Create"); - } - var container = factoryMethod.Invoke(null, new object[0]) as IContainer; + throw new Exception($"Container factory class '{configuredTypeName}' does not have a public static method named Create."); + + var container = factoryMethod.Invoke(null, Array.Empty()) as IContainer; if (container == null) - { - throw new Exception($"Container factory '${configuredTypeName}' did not return an IContainer implementation."); - } + throw new Exception($"Container factory '{configuredTypeName}' did not return an IContainer implementation."); return container; } diff --git a/src/Umbraco.Core/Runtime/CoreRuntimeComponent.cs b/src/Umbraco.Core/Runtime/CoreRuntimeComponent.cs index d06b6215b3..efd9bde0b5 100644 --- a/src/Umbraco.Core/Runtime/CoreRuntimeComponent.cs +++ b/src/Umbraco.Core/Runtime/CoreRuntimeComponent.cs @@ -103,7 +103,7 @@ namespace Umbraco.Core.Runtime composition.Container.RegisterCollectionBuilder() .Add(factory => factory.GetInstance().GetTypes()); - composition.Container.RegisterSingleton(); + composition.Container.RegisterSingleton(factory => new MigrationBuilder(composition.Container)); // by default, register a noop factory composition.Container.RegisterSingleton(); diff --git a/src/Umbraco.Core/UmbracoApplicationBase.cs b/src/Umbraco.Core/UmbracoApplicationBase.cs index 12bb63f83b..2e08814e1f 100644 --- a/src/Umbraco.Core/UmbracoApplicationBase.cs +++ b/src/Umbraco.Core/UmbracoApplicationBase.cs @@ -70,7 +70,6 @@ namespace Umbraco.Core // create the container for the application, and configure. // the boot manager is responsible for registrations var container = GetContainer(); - container.RegisterSingleton(x => container); Current.Container = container; // register the essential stuff, diff --git a/src/Umbraco.Web/UmbracoApplication.cs b/src/Umbraco.Web/UmbracoApplication.cs index 40ebac4536..136f09f83d 100644 --- a/src/Umbraco.Web/UmbracoApplication.cs +++ b/src/Umbraco.Web/UmbracoApplication.cs @@ -1,5 +1,4 @@ using Umbraco.Core; -using Umbraco.Core.Composing; using Umbraco.Web.Runtime; namespace Umbraco.Web @@ -13,10 +12,5 @@ namespace Umbraco.Web { return new WebRuntime(this); } - - protected override IContainer GetContainer() - { - return Composing.LightInject.LightInjectContainer.Create(); - } } }