2020-03-13 18:44:58 +11:00
using LightInject ;
using LightInject.Microsoft.DependencyInjection ;
using Microsoft.Extensions.DependencyInjection ;
using System ;
2020-05-12 15:18:55 +10:00
using Umbraco.Composing ;
2020-03-13 18:44:58 +11:00
using Umbraco.Core.Composing.LightInject ;
2020-05-12 15:18:55 +10:00
using Umbraco.Core.Configuration ;
using Umbraco.Core.IO ;
2020-03-13 18:44:58 +11:00
namespace Umbraco.Core.Composing
{
/// <summary>
/// Used to create Umbraco's container and cross-wire it up before the applicaton starts
/// </summary>
public class UmbracoServiceProviderFactory : IServiceProviderFactory < IServiceContainer >
{
2020-05-12 17:14:39 +10:00
public UmbracoServiceProviderFactory ( ServiceContainer container , bool initializeCurrent )
2020-04-20 12:20:47 +02:00
{
2020-03-13 18:44:58 +11:00
_container = new LightInjectContainer ( container ) ;
2020-05-12 17:14:39 +10:00
_initializeCurrent = initializeCurrent ;
2020-03-13 18:44:58 +11:00
}
2020-03-24 11:59:42 +11:00
/// <summary>
/// Creates an ASP.NET Core compatible service container
/// </summary>
/// <returns></returns>
2020-04-20 12:20:47 +02:00
public static ServiceContainer CreateServiceContainer ( ) = > new ServiceContainer (
ContainerOptions . Default . Clone ( )
. WithMicrosoftSettings ( )
//.WithAspNetCoreSettings() //TODO WithAspNetCoreSettings changes behavior that we need to discuss
) ;
2020-03-24 11:59:42 +11:00
2020-03-13 18:44:58 +11:00
/// <summary>
/// Default ctor for use in Host Builder configuration
/// </summary>
public UmbracoServiceProviderFactory ( )
{
2020-03-24 11:59:42 +11:00
var container = CreateServiceContainer ( ) ;
2020-03-13 18:44:58 +11:00
UmbracoContainer = _container = new LightInjectContainer ( container ) ;
IsActive = true ;
2020-05-12 17:14:39 +10:00
_initializeCurrent = true ;
2020-03-13 18:44:58 +11:00
}
// see here for orig lightinject version https://github.com/seesharper/LightInject.Microsoft.DependencyInjection/blob/412566e3f70625e6b96471db5e1f7cd9e3e1eb18/src/LightInject.Microsoft.DependencyInjection/LightInject.Microsoft.DependencyInjection.cs#L263
// we don't really need all that, we're manually creating our container with the correct options and that
// is what we'll return in CreateBuilder
IServiceCollection _services ;
readonly LightInjectContainer _container ;
2020-05-12 17:14:39 +10:00
private readonly bool _initializeCurrent ;
2020-03-13 18:44:58 +11:00
internal LightInjectContainer GetContainer ( ) = > _container ;
/// <summary>
/// When the empty ctor is used this returns if this factory is active
/// </summary>
public static bool IsActive { get ; private set ; }
/// <summary>
/// When the empty ctor is used this returns the created IRegister
/// </summary>
public static IRegister UmbracoContainer { get ; private set ; }
/// <summary>
/// Create the container with the required settings for aspnetcore3
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public IServiceContainer CreateBuilder ( IServiceCollection services )
{
2020-04-20 12:20:47 +02:00
_services = services ;
2020-03-13 18:44:58 +11:00
return _container . Container ;
}
/// <summary>
/// This cross-wires the container just before the application calls "Configure"
/// </summary>
/// <param name="containerBuilder"></param>
/// <returns></returns>
public IServiceProvider CreateServiceProvider ( IServiceContainer containerBuilder )
{
var provider = containerBuilder . CreateServiceProvider ( _services ) ;
2020-05-12 15:18:55 +10:00
2020-05-12 17:14:39 +10:00
if ( _initializeCurrent )
{
// after cross wiring, configure "Current"
Current . Initialize (
_container . GetInstance < Umbraco . Core . Logging . ILogger > ( ) ,
_container . GetInstance < Configs > ( ) ,
_container . GetInstance < IIOHelper > ( ) ,
_container . GetInstance < Umbraco . Core . Hosting . IHostingEnvironment > ( ) ,
_container . GetInstance < IBackOfficeInfo > ( ) ,
_container . GetInstance < Umbraco . Core . Logging . IProfiler > ( ) ) ;
}
2020-06-17 16:39:28 +02:00
2020-05-12 15:18:55 +10:00
2020-03-13 18:44:58 +11:00
return provider ;
}
}
}