2020-03-25 20:42:02 +01:00
using System ;
using System.Data.Common ;
2020-04-27 10:09:10 +02:00
using System.Data.SqlClient ;
2020-04-03 01:08:52 +11:00
using System.IO ;
2020-03-25 20:42:02 +01:00
using System.Reflection ;
2020-05-19 09:45:31 +02:00
using System.Runtime.InteropServices ;
2020-02-24 16:18:47 +01:00
using Microsoft.AspNetCore.Hosting ;
using Microsoft.AspNetCore.Http ;
2020-03-16 14:02:08 +01:00
using Microsoft.Extensions.Configuration ;
2020-02-18 08:32:06 +01:00
using Microsoft.Extensions.DependencyInjection ;
2020-04-22 14:23:56 +10:00
using Microsoft.Extensions.Logging ;
2020-08-21 14:52:47 +01:00
using Microsoft.Extensions.Options ;
2020-04-22 14:23:56 +10:00
using Serilog ;
using Serilog.Extensions.Hosting ;
using Serilog.Extensions.Logging ;
2020-09-16 13:08:27 +02:00
using Umbraco.Composing ;
2020-03-16 19:14:04 +01:00
using Umbraco.Core ;
2020-02-24 16:18:47 +01:00
using Umbraco.Core.Cache ;
2020-03-13 18:44:58 +11:00
using Umbraco.Core.Composing ;
2020-02-24 16:18:47 +01:00
using Umbraco.Core.Configuration ;
2020-08-21 14:52:47 +01:00
using Umbraco.Core.Configuration.Models ;
2020-09-18 11:30:26 +02:00
using Umbraco.Core.Configuration.Models.Validation ;
2020-02-24 16:18:47 +01:00
using Umbraco.Core.IO ;
using Umbraco.Core.Logging ;
using Umbraco.Core.Logging.Serilog ;
2020-03-13 18:44:58 +11:00
using Umbraco.Core.Persistence ;
2020-05-06 20:37:03 +02:00
using Umbraco.Core.Persistence.SqlSyntax ;
2020-02-25 08:56:58 +01:00
using Umbraco.Core.Runtime ;
2020-03-31 12:22:11 +02:00
using Umbraco.Web.Common.AspNetCore ;
2020-05-12 10:21:40 +10:00
using Umbraco.Web.Common.Profiler ;
2020-08-23 15:58:37 +02:00
using ConnectionStrings = Umbraco . Core . Configuration . Models . ConnectionStrings ;
2020-08-21 14:52:47 +01:00
using CoreDebugSettings = Umbraco . Core . Configuration . Models . CoreDebugSettings ;
2020-09-07 15:28:58 +02:00
using IHostingEnvironment = Umbraco . Core . Hosting . IHostingEnvironment ;
2020-09-16 14:29:33 +02:00
using ILogger = Microsoft . Extensions . Logging . ILogger ;
2020-02-18 08:32:06 +01:00
2020-05-07 10:08:23 +02:00
namespace Umbraco.Extensions
2020-02-18 08:32:06 +01:00
{
2020-03-24 14:48:32 +11:00
public static class UmbracoCoreServiceCollectionExtensions
2020-02-18 08:32:06 +01:00
{
2020-05-12 10:21:40 +10:00
/// <summary>
/// Adds SqlCe support for Umbraco
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
2020-05-06 20:37:03 +02:00
public static IServiceCollection AddUmbracoSqlCeSupport ( this IServiceCollection services )
{
try
{
var binFolder = Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ;
if ( binFolder ! = null )
{
var dllPath = Path . Combine ( binFolder , "Umbraco.Persistance.SqlCe.dll" ) ;
2020-05-08 16:35:37 +10:00
var umbSqlCeAssembly = Assembly . LoadFrom ( dllPath ) ;
2020-05-06 20:37:03 +02:00
2020-05-08 16:35:37 +10:00
var sqlCeSyntaxProviderType = umbSqlCeAssembly . GetType ( "Umbraco.Persistance.SqlCe.SqlCeSyntaxProvider" ) ;
var sqlCeBulkSqlInsertProviderType = umbSqlCeAssembly . GetType ( "Umbraco.Persistance.SqlCe.SqlCeBulkSqlInsertProvider" ) ;
var sqlCeEmbeddedDatabaseCreatorType = umbSqlCeAssembly . GetType ( "Umbraco.Persistance.SqlCe.SqlCeEmbeddedDatabaseCreator" ) ;
2020-05-06 20:37:03 +02:00
if ( ! ( sqlCeSyntaxProviderType is null | | sqlCeBulkSqlInsertProviderType is null | | sqlCeEmbeddedDatabaseCreatorType is null ) )
{
services . AddSingleton ( typeof ( ISqlSyntaxProvider ) , sqlCeSyntaxProviderType ) ;
services . AddSingleton ( typeof ( IBulkSqlInsertProvider ) , sqlCeBulkSqlInsertProviderType ) ;
services . AddSingleton ( typeof ( IEmbeddedDatabaseCreator ) , sqlCeEmbeddedDatabaseCreatorType ) ;
}
2020-05-08 16:35:37 +10:00
var sqlCeAssembly = Assembly . LoadFrom ( Path . Combine ( binFolder , "System.Data.SqlServerCe.dll" ) ) ;
2020-05-06 20:37:03 +02:00
2020-05-08 16:35:37 +10:00
var sqlCe = sqlCeAssembly . GetType ( "System.Data.SqlServerCe.SqlCeProviderFactory" ) ;
2020-05-06 20:37:03 +02:00
if ( ! ( sqlCe is null ) )
{
2020-05-12 10:21:40 +10:00
DbProviderFactories . RegisterFactory ( Core . Constants . DbProviderNames . SqlCe , sqlCe ) ;
2020-05-06 20:37:03 +02:00
}
}
}
catch
{
// Ignore if SqlCE is not available
}
return services ;
}
2020-05-12 10:21:40 +10:00
/// <summary>
/// Adds Sql Server support for Umbraco
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
2020-05-06 20:37:03 +02:00
public static IServiceCollection AddUmbracoSqlServerSupport ( this IServiceCollection services )
{
DbProviderFactories . RegisterFactory ( Core . Constants . DbProviderNames . SqlServer , SqlClientFactory . Instance ) ;
services . AddSingleton < ISqlSyntaxProvider , SqlServerSyntaxProvider > ( ) ;
services . AddSingleton < IBulkSqlInsertProvider , SqlServerBulkSqlInsertProvider > ( ) ;
services . AddSingleton < IEmbeddedDatabaseCreator , NoopEmbeddedDatabaseCreator > ( ) ;
return services ;
}
2020-09-07 15:28:58 +02:00
/// <summary>
/// Adds the Umbraco Back Core requirements
/// </summary>
/// <param name="services"></param>
/// <param name="webHostEnvironment"></param>
/// <param name="umbContainer"></param>
/// <param name="entryAssembly"></param>
/// <param name="appCaches"></param>
/// <param name="loggingConfiguration"></param>
/// <param name="factory"></param>
2020-09-18 08:31:37 +02:00
/// <param name="configuration"></param>
/// <param name="httpContextAccessor"></param>
2020-09-07 15:28:58 +02:00
/// <returns></returns>
2020-09-18 08:31:37 +02:00
public static IServiceCollection AddUmbracoCore ( this IServiceCollection services ,
2020-09-07 15:28:58 +02:00
IWebHostEnvironment webHostEnvironment ,
IRegister umbContainer ,
Assembly entryAssembly ,
AppCaches appCaches ,
ILoggingConfiguration loggingConfiguration ,
2020-09-18 08:31:37 +02:00
IConfiguration configuration ,
2020-09-07 15:28:58 +02:00
out IFactory factory )
2020-09-18 08:31:37 +02:00
= > services . AddUmbracoCore ( webHostEnvironment , umbContainer , entryAssembly , appCaches , loggingConfiguration , configuration , GetCoreRuntime , out factory ) ;
2020-09-07 15:28:58 +02:00
2020-03-24 14:48:32 +11:00
/// <summary>
/// Adds the Umbraco Configuration requirements
/// </summary>
/// <param name="services"></param>
2020-03-25 18:21:44 +11:00
/// <param name="configuration"></param>
2020-03-24 14:48:32 +11:00
/// <returns></returns>
2020-03-25 18:21:44 +11:00
public static IServiceCollection AddUmbracoConfiguration ( this IServiceCollection services , IConfiguration configuration )
2020-03-17 17:56:00 +01:00
{
2020-03-25 18:21:44 +11:00
if ( configuration = = null ) throw new ArgumentNullException ( nameof ( configuration ) ) ;
2020-03-23 16:39:27 +11:00
2020-09-20 23:18:04 +02:00
services . AddSingleton < IValidateOptions < ContentSettings > , ContentSettingsValidator > ( ) ;
services . AddSingleton < IValidateOptions < GlobalSettings > , GlobalSettingsValidator > ( ) ;
services . AddSingleton < IValidateOptions < RequestHandlerSettings > , RequestHandlerSettingsValidator > ( ) ;
2020-03-17 17:56:00 +01:00
2020-09-21 16:23:12 +02:00
services . Configure < ActiveDirectorySettings > ( configuration . GetSection ( Constants . Configuration . ConfigActiveDirectory ) ) ;
2020-08-24 09:29:40 +02:00
services . Configure < ConnectionStrings > ( configuration . GetSection ( "ConnectionStrings" ) , o = > o . BindNonPublicProperties = true ) ;
2020-09-21 21:20:46 +02:00
services . Configure < ContentSettings > ( configuration . GetSection ( Constants . Configuration . ConfigContent ) ) ;
2020-09-21 16:23:12 +02:00
services . Configure < CoreDebugSettings > ( configuration . GetSection ( Constants . Configuration . ConfigCoreDebug ) ) ;
services . Configure < ExceptionFilterSettings > ( configuration . GetSection ( Constants . Configuration . ConfigExceptionFilter ) ) ;
2020-09-21 21:20:46 +02:00
services . Configure < GlobalSettings > ( configuration . GetSection ( Constants . Configuration . ConfigGlobal ) ) ;
2020-09-21 16:23:12 +02:00
services . Configure < HealthChecksSettings > ( configuration . GetSection ( Constants . Configuration . ConfigHealthChecks ) ) ;
2020-09-21 21:20:46 +02:00
services . Configure < HostingSettings > ( configuration . GetSection ( Constants . Configuration . ConfigHosting ) ) ;
2020-09-21 16:23:12 +02:00
services . Configure < ImagingSettings > ( configuration . GetSection ( Constants . Configuration . ConfigImaging ) ) ;
services . Configure < IndexCreatorSettings > ( configuration . GetSection ( Constants . Configuration . ConfigExamine ) ) ;
services . Configure < KeepAliveSettings > ( configuration . GetSection ( Constants . Configuration . ConfigKeepAlive ) ) ;
services . Configure < LoggingSettings > ( configuration . GetSection ( Constants . Configuration . ConfigLogging ) ) ;
services . Configure < MemberPasswordConfigurationSettings > ( configuration . GetSection ( Constants . Configuration . ConfigMemberPassword ) ) ;
services . Configure < ModelsBuilderSettings > ( configuration . GetSection ( Constants . Configuration . ConfigModelsBuilder ) , o = > o . BindNonPublicProperties = true ) ;
services . Configure < NuCacheSettings > ( configuration . GetSection ( Constants . Configuration . ConfigNuCache ) ) ;
services . Configure < RequestHandlerSettings > ( configuration . GetSection ( Constants . Configuration . ConfigRequestHandler ) ) ;
services . Configure < RuntimeSettings > ( configuration . GetSection ( Constants . Configuration . ConfigRuntime ) ) ;
services . Configure < SecuritySettings > ( configuration . GetSection ( Constants . Configuration . ConfigSecurity ) ) ;
services . Configure < TourSettings > ( configuration . GetSection ( Constants . Configuration . ConfigTours ) ) ;
services . Configure < TypeFinderSettings > ( configuration . GetSection ( Constants . Configuration . ConfigTypeFinder ) ) ;
services . Configure < UserPasswordConfigurationSettings > ( configuration . GetSection ( Constants . Configuration . ConfigUserPassword ) ) ;
services . Configure < WebRoutingSettings > ( configuration . GetSection ( Constants . Configuration . ConfigWebRouting ) ) ;
2020-03-17 17:56:00 +01:00
return services ;
}
2020-03-13 18:44:58 +11:00
/// <summary>
2020-03-24 14:48:32 +11:00
/// Adds the Umbraco Back Core requirements
2020-03-13 18:44:58 +11:00
/// </summary>
/// <param name="services"></param>
2020-03-25 18:21:44 +11:00
/// <param name="webHostEnvironment"></param>
2020-09-17 09:01:10 +02:00
/// <param name="configuration"></param>
2020-03-13 18:44:58 +11:00
/// <returns></returns>
2020-09-17 09:01:10 +02:00
public static IServiceCollection AddUmbracoCore ( this IServiceCollection services , IWebHostEnvironment webHostEnvironment , IConfiguration configuration )
2020-04-01 14:19:41 +02:00
{
2020-09-17 09:01:10 +02:00
return services . AddUmbracoCore ( webHostEnvironment , configuration , out _ ) ;
2020-04-01 14:19:41 +02:00
}
/// <summary>
/// Adds the Umbraco Back Core requirements
/// </summary>
/// <param name="services"></param>
/// <param name="webHostEnvironment"></param>
2020-09-17 09:01:10 +02:00
/// <param name="configuration"></param>
2020-04-01 14:19:41 +02:00
/// <param name="factory"></param>
/// <returns></returns>
2020-09-17 09:01:10 +02:00
public static IServiceCollection AddUmbracoCore ( this IServiceCollection services , IWebHostEnvironment webHostEnvironment , IConfiguration configuration , out IFactory factory )
2020-03-13 18:44:58 +11:00
{
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 ;
2020-04-22 14:23:56 +10:00
var loggingConfig = new LoggingConfiguration (
2020-09-17 09:01:10 +02:00
Path . Combine ( webHostEnvironment . ContentRootPath , "umbraco" , "logs" ) ) ;
2020-04-22 14:23:56 +10:00
2020-04-20 06:19:59 +02:00
IHttpContextAccessor httpContextAccessor = new HttpContextAccessor ( ) ;
services . AddSingleton < IHttpContextAccessor > ( httpContextAccessor ) ;
2020-09-17 09:01:10 +02:00
services . AddSingleton < ILoggingConfiguration > ( loggingConfig ) ;
2020-09-04 00:27:43 +10:00
2020-04-28 12:31:15 +02:00
var requestCache = new GenericDictionaryRequestAppCache ( ( ) = > httpContextAccessor . HttpContext ? . Items ) ;
2020-09-04 00:27:43 +10:00
var appCaches = new AppCaches (
new DeepCloneAppCache ( new ObjectCacheAppCache ( ) ) ,
requestCache ,
new IsolatedCaches ( type = > new DeepCloneAppCache ( new ObjectCacheAppCache ( ) ) ) ) ;
2020-04-20 06:19:59 +02:00
2020-04-22 10:29:21 +02:00
services . AddUmbracoCore ( webHostEnvironment ,
umbContainer ,
Assembly . GetEntryAssembly ( ) ,
2020-09-04 00:27:43 +10:00
appCaches ,
2020-04-22 10:29:21 +02:00
loggingConfig ,
2020-09-18 08:31:37 +02:00
configuration ,
2020-09-07 15:28:58 +02:00
GetCoreRuntime ,
2020-04-22 10:29:21 +02:00
out factory ) ;
2020-03-25 20:42:02 +01:00
return services ;
2020-08-19 09:39:11 +02:00
}
2020-03-13 19:10:21 +11:00
2020-03-24 14:48:32 +11:00
/// <summary>
/// Adds the Umbraco Back Core requirements
/// </summary>
/// <param name="services"></param>
2020-03-25 18:21:44 +11:00
/// <param name="webHostEnvironment"></param>
2020-03-24 14:48:32 +11:00
/// <param name="umbContainer"></param>
/// <param name="entryAssembly"></param>
2020-09-07 15:28:58 +02:00
/// <param name="requestCache"></param>
2020-09-02 18:10:29 +10:00
/// <param name="httpContextAccessor"></param>
/// <param name="loggingConfiguration"></param>
/// <param name="getRuntime">Delegate to create an <see cref="IRuntime"/></param>
/// <param name="factory"></param>
/// <returns></returns>
public static IServiceCollection AddUmbracoCore (
this IServiceCollection services ,
IWebHostEnvironment webHostEnvironment ,
IRegister umbContainer ,
Assembly entryAssembly ,
2020-09-07 15:28:58 +02:00
AppCaches appCaches ,
2020-09-02 18:10:29 +10:00
ILoggingConfiguration loggingConfiguration ,
2020-09-17 09:01:10 +02:00
IConfiguration configuration ,
2020-09-07 15:28:58 +02:00
//TODO: Yep that's extremely ugly
2020-10-06 21:23:15 +02:00
Func < GlobalSettings , ConnectionStrings , IUmbracoVersion , IIOHelper , ILoggerFactory , IProfiler , IHostingEnvironment , IBackOfficeInfo , ITypeFinder , AppCaches , IDbProviderFactoryCreator , IRuntime > getRuntime ,
2020-09-02 18:10:29 +10:00
out IFactory factory )
2020-02-18 08:32:06 +01:00
{
2020-03-24 11:53:56 +11:00
if ( services is null ) throw new ArgumentNullException ( nameof ( services ) ) ;
2020-03-27 11:39:17 +01:00
var container = umbContainer ;
if ( container is null ) throw new ArgumentNullException ( nameof ( container ) ) ;
2020-03-24 11:53:56 +11:00
if ( entryAssembly is null ) throw new ArgumentNullException ( nameof ( entryAssembly ) ) ;
2020-08-04 12:54:54 +02:00
// Add supported databases
2020-05-08 17:30:30 +10:00
services . AddUmbracoSqlCeSupport ( ) ;
services . AddUmbracoSqlServerSupport ( ) ;
2020-05-06 20:37:03 +02:00
services . AddSingleton < IDbProviderFactoryCreator > ( x = > new DbProviderFactoryCreator (
DbProviderFactories . GetFactory ,
x . GetServices < ISqlSyntaxProvider > ( ) ,
x . GetServices < IBulkSqlInsertProvider > ( ) ,
x . GetServices < IEmbeddedDatabaseCreator > ( )
) ) ;
2020-05-08 17:47:21 +10:00
// TODO: We want to avoid pre-resolving a container as much as possible we should not
// be doing this any more than we are now. The ugly part about this is that the service
// instances resolved here won't be the same instances resolved from the container
// later once the true container is built. However! ... in the case of IDbProviderFactoryCreator
// it will be the same instance resolved later because we are re-registering this instance back
// into the container. This is not true for `Configs` but we should do that too, see comments in
// `RegisterEssentials`.
2020-04-20 06:19:59 +02:00
var serviceProvider = services . BuildServiceProvider ( ) ;
2020-09-15 15:14:44 +02:00
2020-09-03 12:29:23 +02:00
var globalSettings = serviceProvider . GetService < IOptionsMonitor < GlobalSettings > > ( ) ;
2020-09-03 11:36:57 +02:00
var connectionStrings = serviceProvider . GetService < IOptions < ConnectionStrings > > ( ) ;
2020-09-03 12:29:23 +02:00
var hostingSettings = serviceProvider . GetService < IOptionsMonitor < HostingSettings > > ( ) ;
var typeFinderSettings = serviceProvider . GetService < IOptionsMonitor < TypeFinderSettings > > ( ) ;
2020-08-21 14:52:47 +01:00
2020-04-29 08:56:42 +02:00
var dbProviderFactoryCreator = serviceProvider . GetRequiredService < IDbProviderFactoryCreator > ( ) ;
2020-02-24 16:18:47 +01:00
2020-04-22 10:29:21 +02:00
CreateCompositionRoot ( services ,
2020-08-21 14:52:47 +01:00
globalSettings ,
hostingSettings ,
2020-04-22 10:29:21 +02:00
webHostEnvironment ,
loggingConfiguration ,
2020-10-06 21:23:15 +02:00
configuration , out var ioHelper , out var hostingEnvironment , out var backOfficeInfo , out var profiler ) ;
var loggerFactory = serviceProvider . GetService < ILoggerFactory > ( ) ;
2020-03-13 19:10:21 +11:00
2020-09-03 11:36:57 +02:00
var umbracoVersion = new UmbracoVersion ( ) ;
2020-09-24 09:39:48 +02:00
var typeFinder = CreateTypeFinder ( loggerFactory , profiler , webHostEnvironment , entryAssembly , typeFinderSettings ) ;
2020-03-13 18:44:58 +11:00
2020-09-07 15:28:58 +02:00
var coreRuntime = getRuntime (
2020-09-03 12:29:23 +02:00
globalSettings . CurrentValue ,
2020-09-03 11:36:57 +02:00
connectionStrings . Value ,
2020-03-13 18:44:58 +11:00
umbracoVersion ,
2020-03-24 11:53:56 +11:00
ioHelper ,
2020-09-15 15:14:44 +02:00
loggerFactory ,
2020-03-24 11:53:56 +11:00
profiler ,
hostingEnvironment ,
backOfficeInfo ,
2020-04-27 10:09:10 +02:00
typeFinder ,
2020-09-04 00:27:43 +10:00
appCaches ,
2020-04-28 07:01:30 +02:00
dbProviderFactoryCreator ) ;
2020-03-13 18:44:58 +11:00
2020-09-07 15:28:58 +02:00
factory = coreRuntime . Configure ( container ) ;
2020-03-13 18:44:58 +11:00
2020-09-07 15:28:58 +02:00
services . Configure < HostingSettings > ( hostingSettings = >
{
hostingSettings . Debug = false ;
} ) ;
2020-03-13 18:44:58 +11:00
2020-02-18 08:32:06 +01:00
return services ;
2020-06-30 20:11:39 +02:00
}
2020-05-06 20:37:03 +02:00
2020-09-18 12:53:06 +02:00
private static ITypeFinder CreateTypeFinder ( ILoggerFactory loggerFactory , IProfiler profiler , IWebHostEnvironment webHostEnvironment , Assembly entryAssembly , IOptionsMonitor < TypeFinderSettings > typeFinderSettings )
2020-04-03 01:08:52 +11:00
{
var runtimeHashPaths = new RuntimeHashPaths ( ) ;
runtimeHashPaths . AddFolder ( new DirectoryInfo ( Path . Combine ( webHostEnvironment . ContentRootPath , "bin" ) ) ) ;
2020-09-16 14:29:33 +02:00
var runtimeHash = new RuntimeHash ( new ProfilingLogger ( loggerFactory . CreateLogger ( "RuntimeHash" ) , profiler ) , runtimeHashPaths ) ;
2020-09-16 13:08:27 +02:00
return new TypeFinder ( loggerFactory . CreateLogger < TypeFinder > ( ) , new DefaultUmbracoAssemblyProvider ( entryAssembly ) , runtimeHash , new TypeFinderConfig ( typeFinderSettings ) ) ;
2020-04-03 01:08:52 +11:00
}
2020-04-20 06:19:59 +02:00
private static IRuntime GetCoreRuntime (
2020-10-06 21:23:15 +02:00
GlobalSettings globalSettings , ConnectionStrings connectionStrings , IUmbracoVersion umbracoVersion , IIOHelper ioHelper , ILoggerFactory loggerFactory ,
2020-09-08 13:03:43 +02:00
IProfiler profiler , IHostingEnvironment hostingEnvironment , IBackOfficeInfo backOfficeInfo ,
2020-09-04 00:27:43 +10:00
ITypeFinder typeFinder , AppCaches appCaches , IDbProviderFactoryCreator dbProviderFactoryCreator )
2020-03-13 18:44:58 +11:00
{
// Determine if we should use the sql main dom or the default
2020-03-23 15:50:01 +11:00
var appSettingMainDomLock = globalSettings . MainDomLock ;
2020-06-25 11:39:11 +02:00
2020-08-19 09:39:11 +02:00
var isWindows = RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ;
var mainDomLock = appSettingMainDomLock = = "SqlMainDomLock" | | isWindows = = false
2020-09-18 12:53:06 +02:00
? ( IMainDomLock ) new SqlMainDomLock ( loggerFactory . CreateLogger < SqlMainDomLock > ( ) , loggerFactory , globalSettings , connectionStrings , dbProviderFactoryCreator , hostingEnvironment )
2020-09-16 14:29:33 +02:00
: new MainDomSemaphoreLock ( loggerFactory . CreateLogger < MainDomSemaphoreLock > ( ) , hostingEnvironment ) ;
2020-03-13 18:44:58 +11:00
2020-09-16 14:29:33 +02:00
var mainDom = new MainDom ( loggerFactory . CreateLogger < MainDom > ( ) , mainDomLock ) ;
2020-03-13 18:44:58 +11:00
2020-04-20 06:19:59 +02:00
var coreRuntime = new CoreRuntime (
2020-08-21 14:52:47 +01:00
globalSettings ,
connectionStrings ,
2020-04-20 06:19:59 +02:00
umbracoVersion ,
ioHelper ,
2020-09-15 15:14:44 +02:00
loggerFactory ,
2020-04-20 06:19:59 +02:00
profiler ,
new AspNetCoreBootPermissionsChecker ( ) ,
hostingEnvironment ,
backOfficeInfo ,
dbProviderFactoryCreator ,
mainDom ,
typeFinder ,
2020-09-04 00:27:43 +10:00
appCaches ) ;
2020-03-13 18:44:58 +11:00
return coreRuntime ;
}
2020-02-25 08:56:58 +01:00
2020-04-22 14:23:56 +10:00
private static IServiceCollection CreateCompositionRoot (
IServiceCollection services ,
2020-09-03 12:29:23 +02:00
IOptionsMonitor < GlobalSettings > globalSettings ,
IOptionsMonitor < HostingSettings > hostingSettings ,
2020-04-22 14:23:56 +10:00
IWebHostEnvironment webHostEnvironment ,
2020-04-22 10:29:21 +02:00
ILoggingConfiguration loggingConfiguration ,
2020-09-17 09:01:10 +02:00
IConfiguration configuration ,
2020-04-22 10:29:21 +02:00
out IIOHelper ioHelper ,
out Core . Hosting . IHostingEnvironment hostingEnvironment ,
out IBackOfficeInfo backOfficeInfo ,
out IProfiler profiler )
2020-02-24 16:18:47 +01:00
{
2020-08-21 14:52:47 +01:00
if ( globalSettings = = null )
throw new InvalidOperationException ( $"Could not resolve type {typeof(GlobalSettings)} from the container, ensure {nameof(AddUmbracoConfiguration)} is called before calling {nameof(AddUmbracoCore)}" ) ;
2020-02-24 16:18:47 +01:00
2020-04-03 01:08:52 +11:00
hostingEnvironment = new AspNetCoreHostingEnvironment ( hostingSettings , webHostEnvironment ) ;
2020-09-04 01:30:47 +10:00
ioHelper = new IOHelper ( hostingEnvironment ) ;
2020-10-06 21:23:15 +02:00
AddLogger ( services , hostingEnvironment , loggingConfiguration , configuration ) ;
2020-03-24 14:47:10 +11:00
backOfficeInfo = new AspNetCoreBackOfficeInfo ( globalSettings ) ;
2020-04-22 15:23:51 +10:00
profiler = GetWebProfiler ( hostingEnvironment ) ;
2020-02-24 16:18:47 +01:00
2020-03-16 14:02:08 +01:00
return services ;
2020-02-24 16:18:47 +01:00
}
2020-03-24 10:51:53 +01:00
2020-04-22 14:23:56 +10:00
/// <summary>
/// Create and configure the logger
/// </summary>
/// <param name="hostingEnvironment"></param>
2020-10-06 21:23:15 +02:00
private static void AddLogger (
2020-09-24 09:39:48 +02:00
IServiceCollection services ,
Core . Hosting . IHostingEnvironment hostingEnvironment ,
ILoggingConfiguration loggingConfiguration ,
2020-10-06 21:23:15 +02:00
IConfiguration configuration )
2020-04-22 14:23:56 +10:00
{
// Create a serilog logger
2020-09-17 09:01:10 +02:00
var logger = SerilogLogger . CreateWithDefaultConfiguration ( hostingEnvironment , loggingConfiguration , configuration ) ;
// This is nessasary to pick up all the loggins to MS ILogger.
Log . Logger = logger . SerilogLog ;
2020-09-16 14:29:33 +02:00
2020-04-22 14:23:56 +10:00
// Wire up all the bits that serilog needs. We need to use our own code since the Serilog ext methods don't cater to our needs since
// we don't want to use the global serilog `Log` object and we don't have our own ILogger implementation before the HostBuilder runs which
2020-04-22 10:29:21 +02:00
// is the only other option that these ext methods allow.
2020-04-22 14:23:56 +10:00
// I have created a PR to make this nicer https://github.com/serilog/serilog-extensions-hosting/pull/19 but we'll need to wait for that.
// Also see : https://github.com/serilog/serilog-extensions-hosting/blob/dev/src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs
2020-09-17 09:59:13 +02:00
services . AddLogging ( configure = >
2020-09-16 14:29:33 +02:00
{
2020-09-17 09:59:13 +02:00
configure . AddSerilog ( logger . SerilogLog , false ) ;
2020-09-16 14:29:33 +02:00
} ) ;
2020-10-06 21:23:15 +02:00
// This won't (and shouldn't) take ownership of the logger.
services . AddSingleton ( logger . SerilogLog ) ;
2020-09-17 09:01:10 +02:00
2020-10-06 21:23:15 +02:00
// Registered to provide two services...
var diagnosticContext = new DiagnosticContext ( logger . SerilogLog ) ;
2020-04-22 14:23:56 +10:00
2020-10-06 21:23:15 +02:00
// Consumed by e.g. middleware
services . AddSingleton ( diagnosticContext ) ;
// Consumed by user code
services . AddSingleton < IDiagnosticContext > ( diagnosticContext ) ;
2020-04-22 14:23:56 +10:00
}
2020-04-22 15:23:51 +10:00
private static IProfiler GetWebProfiler ( Umbraco . Core . Hosting . IHostingEnvironment hostingEnvironment )
2020-03-25 05:39:25 +01:00
{
// create and start asap to profile boot
if ( ! hostingEnvironment . IsDebugMode )
{
// should let it be null, that's how MiniProfiler is meant to work,
// but our own IProfiler expects an instance so let's get one
return new VoidProfiler ( ) ;
}
2020-04-22 15:23:51 +10:00
var webProfiler = new WebProfiler ( ) ;
2020-03-29 22:35:52 +02:00
webProfiler . StartBoot ( ) ;
2020-03-25 05:39:25 +01:00
return webProfiler ;
}
2020-05-12 10:21:40 +10:00
2020-03-13 18:44:58 +11:00
private class AspNetCoreBootPermissionsChecker : IUmbracoBootPermissionChecker
{
public void ThrowIfNotPermissions ( )
{
// nothing to check
}
}
2020-03-24 14:48:32 +11:00
2020-02-18 08:32:06 +01:00
}
2020-04-20 06:19:59 +02:00
2020-02-18 08:32:06 +01:00
}