Cleanup AddUmbraco & AddUmbracoCore
This commit is contained in:
@@ -64,5 +64,13 @@ namespace Umbraco.Core.Cache
|
||||
/// search through all keys on a global scale.</para>
|
||||
/// </remarks>
|
||||
public IsolatedCaches IsolatedCaches { get; }
|
||||
|
||||
public static AppCaches Create(IRequestCache requestCache)
|
||||
{
|
||||
return new AppCaches(
|
||||
new DeepCloneAppCache(new ObjectCacheAppCache()),
|
||||
requestCache,
|
||||
new IsolatedCaches(type => new DeepCloneAppCache(new ObjectCacheAppCache())));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,13 +61,13 @@ namespace Umbraco.Tests.Integration
|
||||
services.AddRequiredNetCoreServices(testHelper, webHostEnvironment);
|
||||
|
||||
// Add it!
|
||||
var typeLoader = UmbracoCoreServiceCollectionExtensions.CreateTypeLoader(GetType().Assembly, webHostEnvironment,
|
||||
var typeLoader = services.AddTypeLoader(GetType().Assembly, webHostEnvironment,
|
||||
testHelper.ConsoleLoggerFactory, AppCaches.NoCache, hostContext.Configuration);
|
||||
|
||||
var builder = new UmbracoBuilder(services, hostContext.Configuration, typeLoader, testHelper.ConsoleLoggerFactory);
|
||||
builder.Services.AddUnique<AppCaches>(AppCaches.NoCache);
|
||||
builder.AddConfiguration();
|
||||
builder.AddUmbracoCore(webHostEnvironment, GetType().Assembly, testHelper.GetLoggingConfiguration(), hostContext.Configuration);
|
||||
builder.AddUmbracoCore();
|
||||
});
|
||||
|
||||
var host = await hostBuilder.StartAsync();
|
||||
@@ -105,14 +105,14 @@ namespace Umbraco.Tests.Integration
|
||||
|
||||
// Add it!
|
||||
|
||||
var typeLoader = UmbracoCoreServiceCollectionExtensions.CreateTypeLoader(GetType().Assembly,
|
||||
var typeLoader = services.AddTypeLoader(GetType().Assembly,
|
||||
webHostEnvironment, testHelper.ConsoleLoggerFactory, AppCaches.NoCache,
|
||||
hostContext.Configuration);
|
||||
|
||||
var builder = new UmbracoBuilder(services, hostContext.Configuration, typeLoader, testHelper.ConsoleLoggerFactory);
|
||||
builder.Services.AddUnique<AppCaches>(AppCaches.NoCache);
|
||||
builder.AddConfiguration()
|
||||
.AddUmbracoCore(webHostEnvironment, GetType().Assembly, testHelper.GetLoggingConfiguration(), hostContext.Configuration)
|
||||
.AddUmbracoCore()
|
||||
.Build();
|
||||
|
||||
services.AddRouting(); // LinkGenerator
|
||||
|
||||
@@ -5,6 +5,7 @@ using Umbraco.Core.Cache;
|
||||
using Umbraco.Core.Runtime;
|
||||
using Umbraco.Extensions;
|
||||
using Umbraco.Tests.Integration.Implementations;
|
||||
using Umbraco.Web.Common.Builder;
|
||||
|
||||
namespace Umbraco.Tests.Integration.TestServerTest
|
||||
{
|
||||
@@ -17,11 +18,7 @@ namespace Umbraco.Tests.Integration.TestServerTest
|
||||
/// <returns></returns>
|
||||
public static IUmbracoBuilder AddTestCore(this IUmbracoBuilder builder, TestHelper testHelper)
|
||||
{
|
||||
builder.AddUmbracoCore(
|
||||
testHelper.GetWebHostEnvironment(),
|
||||
typeof(UmbracoBuilderExtensions).Assembly,
|
||||
testHelper.GetLoggingConfiguration(),
|
||||
builder.Config);
|
||||
builder.AddUmbracoCore();
|
||||
|
||||
builder.Services.AddUnique<AppCaches>(AppCaches.NoCache);
|
||||
|
||||
|
||||
@@ -130,9 +130,12 @@ namespace Umbraco.Tests.Integration.TestServerTest
|
||||
|
||||
public override void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
var umbracoBuilder = services.AddUmbraco(TestHelper.GetWebHostEnvironment(), Configuration, assembly: GetType().Assembly);
|
||||
var typeLoader = services.AddTypeLoader(GetType().Assembly, TestHelper.GetWebHostEnvironment(),
|
||||
TestHelper.ConsoleLoggerFactory, AppCaches.NoCache, Configuration);
|
||||
|
||||
var builder = new UmbracoBuilder(services, Configuration, typeLoader);
|
||||
|
||||
umbracoBuilder
|
||||
builder
|
||||
.AddConfiguration()
|
||||
.AddTestCore(TestHelper) // This is the important one!
|
||||
.AddWebComponents()
|
||||
|
||||
@@ -174,17 +174,14 @@ namespace Umbraco.Tests.Integration.Testing
|
||||
|
||||
// Add it!
|
||||
|
||||
var typeLoader = UmbracoCoreServiceCollectionExtensions.CreateTypeLoader(GetType().Assembly, webHostEnvironment, TestHelper.ConsoleLoggerFactory, AppCaches.NoCache, Configuration);
|
||||
var typeLoader = services.AddTypeLoader(GetType().Assembly, webHostEnvironment, TestHelper.ConsoleLoggerFactory, AppCaches.NoCache, Configuration);
|
||||
var builder = new UmbracoBuilder(services, Configuration, typeLoader, TestHelper.ConsoleLoggerFactory);
|
||||
|
||||
|
||||
|
||||
builder.Services.AddLogger(TestHelper.GetLoggingConfiguration(), Configuration);
|
||||
|
||||
builder.AddConfiguration()
|
||||
.AddUmbracoCore(
|
||||
webHostEnvironment,
|
||||
GetType().Assembly,
|
||||
TestHelper.GetLoggingConfiguration(),
|
||||
Configuration
|
||||
);
|
||||
.AddUmbracoCore();
|
||||
|
||||
builder.Services.AddUnique<AppCaches>(GetAppCaches());
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Umbraco.Core.Builder;
|
||||
@@ -11,11 +10,11 @@ namespace Umbraco.Extensions
|
||||
{
|
||||
public static class UmbracoBuilderExtensions
|
||||
{
|
||||
public static IUmbracoBuilder AddAllBackOfficeComponents(this IUmbracoBuilder builder, IWebHostEnvironment webHostEnvironment)
|
||||
public static IUmbracoBuilder AddAllBackOfficeComponents(this IUmbracoBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.AddConfiguration()
|
||||
.AddCore(webHostEnvironment)
|
||||
.AddUmbracoCore()
|
||||
.AddWebComponents()
|
||||
.AddRuntimeMinifier()
|
||||
.AddBackOffice()
|
||||
|
||||
@@ -5,8 +5,12 @@ using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Data.SqlClient;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
@@ -19,13 +23,22 @@ using Umbraco.Core;
|
||||
using Umbraco.Core.Builder;
|
||||
using Umbraco.Core.Cache;
|
||||
using Umbraco.Core.Composing;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Configuration.Models;
|
||||
using Umbraco.Core.Configuration.Models.Validation;
|
||||
using Umbraco.Core.IO;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Persistence;
|
||||
using Umbraco.Core.Persistence.SqlSyntax;
|
||||
using Umbraco.Core.Runtime;
|
||||
using Umbraco.Extensions;
|
||||
using Umbraco.Infrastructure.HostedServices;
|
||||
using Umbraco.Infrastructure.HostedServices.ServerRegistration;
|
||||
using Umbraco.Web.Common.ApplicationModels;
|
||||
using Umbraco.Web.Common.AspNetCore;
|
||||
using Umbraco.Web.Common.Filters;
|
||||
using Umbraco.Web.Common.ModelBinders;
|
||||
using IHostingEnvironment = Umbraco.Core.Hosting.IHostingEnvironment;
|
||||
|
||||
namespace Umbraco.Web.Common.Builder
|
||||
{
|
||||
@@ -35,37 +48,129 @@ namespace Umbraco.Web.Common.Builder
|
||||
public static IUmbracoBuilder AddUmbraco(
|
||||
this IServiceCollection services,
|
||||
IWebHostEnvironment webHostEnvironment,
|
||||
IConfiguration config,
|
||||
ILoggingConfiguration loggingConfig = null,
|
||||
ILoggerFactory loggerFactory = null,
|
||||
Assembly assembly = null)
|
||||
IConfiguration config)
|
||||
{
|
||||
if (services is null) throw new ArgumentNullException(nameof(services));
|
||||
if (config is null) throw new ArgumentNullException(nameof(config));
|
||||
|
||||
services.AddLazySupport();
|
||||
|
||||
loggingConfig ??= new LoggingConfiguration(Path.Combine(webHostEnvironment.ContentRootPath, "umbraco", "logs"));
|
||||
var loggingConfig = new LoggingConfiguration(Path.Combine(webHostEnvironment.ContentRootPath, "umbraco", "logs"));
|
||||
services.AddSingleton<ILoggingConfiguration>(loggingConfig);
|
||||
services.AddLogger(loggingConfig, config);
|
||||
loggerFactory ??= LoggerFactory.Create(cfg => cfg.AddSerilog(Log.Logger, false));
|
||||
assembly ??= Assembly.GetEntryAssembly();
|
||||
|
||||
IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();
|
||||
services.AddSingleton<IHttpContextAccessor>(httpContextAccessor);
|
||||
services.AddSingleton(httpContextAccessor);
|
||||
|
||||
var requestCache = new GenericDictionaryRequestAppCache(() => httpContextAccessor.HttpContext?.Items);
|
||||
var appCaches = new AppCaches(
|
||||
new DeepCloneAppCache(new ObjectCacheAppCache()),
|
||||
requestCache,
|
||||
new IsolatedCaches(type => new DeepCloneAppCache(new ObjectCacheAppCache())));
|
||||
|
||||
var appCaches = AppCaches.Create(requestCache);
|
||||
services.AddUnique<AppCaches>(appCaches);
|
||||
|
||||
var typeLoader = UmbracoCoreServiceCollectionExtensions.CreateTypeLoader(assembly, webHostEnvironment, loggerFactory, appCaches, config);
|
||||
var loggerFactory = LoggerFactory.Create(cfg => cfg.AddSerilog(Log.Logger, false));
|
||||
var typeLoader = services.AddTypeLoader(Assembly.GetEntryAssembly(), webHostEnvironment, loggerFactory, appCaches, config);
|
||||
|
||||
return new UmbracoBuilder(services, config, typeLoader, loggerFactory);
|
||||
}
|
||||
|
||||
/// <remarks>Composes Composers</remarks>
|
||||
public static IUmbracoBuilder AddUmbracoCore(this IUmbracoBuilder builder)
|
||||
{
|
||||
if (builder is null) throw new ArgumentNullException(nameof(builder));
|
||||
|
||||
builder.Services.AddLazySupport();
|
||||
|
||||
// Add service session
|
||||
// This can be overwritten by the user by adding their own call to AddSession
|
||||
// since the last call of AddSession take precedence
|
||||
builder.Services.AddSession(options =>
|
||||
{
|
||||
options.Cookie.Name = "UMB_SESSION";
|
||||
options.Cookie.HttpOnly = true;
|
||||
});
|
||||
|
||||
var syntaxProviders = new List<ISqlSyntaxProvider>();
|
||||
var insertProviders = new List<IBulkSqlInsertProvider>();
|
||||
var databaseCreators = new List<IEmbeddedDatabaseCreator>();
|
||||
|
||||
// Add supported databases
|
||||
builder.Services.AddUmbracoSqlCeSupport(syntaxProviders, insertProviders, databaseCreators);
|
||||
builder.Services.AddUmbracoSqlServerSupport(syntaxProviders, insertProviders, databaseCreators);
|
||||
|
||||
builder.Services.AddUnique<IAppPolicyCache>(factory => factory.GetRequiredService<AppCaches>().RuntimeCache);
|
||||
builder.Services.AddUnique<IRequestCache>(factory => factory.GetRequiredService<AppCaches>().RequestCache);
|
||||
|
||||
var dbProviderFactoryCreator = new DbProviderFactoryCreator(
|
||||
DbProviderFactories.GetFactory,
|
||||
syntaxProviders,
|
||||
insertProviders,
|
||||
databaseCreators);
|
||||
|
||||
builder.Services.AddSingleton<IDbProviderFactoryCreator>(dbProviderFactoryCreator);
|
||||
|
||||
builder.Services.AddUnique<IIOHelper, IOHelper>();
|
||||
|
||||
var profiler = UmbracoCoreServiceCollectionExtensions.GetWebProfiler(builder.Config);
|
||||
builder.Services.AddUnique<IProfiler>(profiler);
|
||||
|
||||
builder.Services.AddUnique<IProfilingLogger, ProfilingLogger>();
|
||||
builder.Services.AddUnique<TypeLoader>(builder.TypeLoader);
|
||||
builder.Services.AddUnique<IBackOfficeInfo, AspNetCoreBackOfficeInfo>();
|
||||
|
||||
builder.Services.AddUnique<IUmbracoDatabaseFactory, UmbracoDatabaseFactory>();
|
||||
builder.Services.AddUnique<ISqlContext>(factory => factory.GetRequiredService<IUmbracoDatabaseFactory>().SqlContext);
|
||||
builder.Services.AddUnique<IBulkSqlInsertProvider>(factory => factory.GetRequiredService<IUmbracoDatabaseFactory>().BulkSqlInsertProvider);
|
||||
|
||||
builder.Services.AddUnique<IUmbracoVersion, UmbracoVersion>();
|
||||
|
||||
builder.Services.AddUnique<IDbProviderFactoryCreator>(dbProviderFactoryCreator);
|
||||
builder.Services.AddUnique<IRuntime, CoreRuntime>();
|
||||
builder.Services.AddUnique<IRuntimeState, RuntimeState>();
|
||||
builder.Services.AddUnique<Core.Hosting.IHostingEnvironment, AspNetCoreHostingEnvironment>();
|
||||
|
||||
builder.Services.AddUnique<IMainDomLock>(factory =>
|
||||
{
|
||||
var globalSettings = factory.GetRequiredService<IOptions<GlobalSettings>>().Value;
|
||||
var connectionStrings = factory.GetRequiredService<IOptions<ConnectionStrings>>().Value;
|
||||
var hostingEnvironment = factory.GetRequiredService<IHostingEnvironment>();
|
||||
|
||||
var dbCreator = factory.GetRequiredService<IDbProviderFactoryCreator>();
|
||||
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
||||
|
||||
return globalSettings.MainDomLock.Equals("SqlMainDomLock") || isWindows == false
|
||||
? (IMainDomLock)new SqlMainDomLock(builder.BuilderLoggerFactory.CreateLogger<SqlMainDomLock>(), builder.BuilderLoggerFactory, globalSettings, connectionStrings, dbCreator, hostingEnvironment)
|
||||
: new MainDomSemaphoreLock(builder.BuilderLoggerFactory.CreateLogger<MainDomSemaphoreLock>(), hostingEnvironment);
|
||||
});
|
||||
|
||||
builder.Services.AddUnique<IMainDom, MainDom>();
|
||||
|
||||
builder.Services.AddUnique<IUmbracoBootPermissionChecker>(new UmbracoCoreServiceCollectionExtensions.AspNetCoreBootPermissionsChecker());
|
||||
|
||||
builder.AddComposers();
|
||||
|
||||
var exceptionLogger = builder.BuilderLoggerFactory.CreateLogger<object>();
|
||||
|
||||
// This probably shouldn't live here.
|
||||
AppDomain.CurrentDomain.UnhandledException += (_, args) =>
|
||||
{
|
||||
var exception = (Exception)args.ExceptionObject;
|
||||
var isTerminating = args.IsTerminating; // always true?
|
||||
|
||||
var msg = "Unhandled exception in AppDomain";
|
||||
if (isTerminating) msg += " (terminating)";
|
||||
msg += ".";
|
||||
exceptionLogger.LogError(exception, msg);
|
||||
};
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static IUmbracoBuilder AddComposers(this IUmbracoBuilder builder)
|
||||
{
|
||||
var composerTypes = builder.TypeLoader.GetTypes<IComposer>();
|
||||
var enableDisable = builder.TypeLoader.GetAssemblyAttributes(typeof(EnableComposerAttribute), typeof(DisableComposerAttribute));
|
||||
new Composers(builder, composerTypes, enableDisable, builder.BuilderLoggerFactory.CreateLogger<Composers>()).Compose();
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static IUmbracoBuilder AddConfiguration(this IUmbracoBuilder builder)
|
||||
{
|
||||
// Register configuration validators.
|
||||
@@ -101,30 +206,22 @@ namespace Umbraco.Web.Common.Builder
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static IUmbracoBuilder AddCore(this IUmbracoBuilder builder, IWebHostEnvironment webHostEnvironment)
|
||||
{
|
||||
var loggingConfig = new LoggingConfiguration(Path.Combine(webHostEnvironment.ContentRootPath, "umbraco", "logs"));
|
||||
|
||||
|
||||
builder.Services.AddSingleton<ILoggingConfiguration>(loggingConfig);
|
||||
|
||||
builder.AddUmbracoCore(webHostEnvironment,
|
||||
Assembly.GetEntryAssembly(),
|
||||
loggingConfig,
|
||||
builder.Config);
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static IUmbracoBuilder AddHostedServices(this IUmbracoBuilder builder)
|
||||
{
|
||||
builder.Services.AddUmbracoHostedServices();
|
||||
builder.Services.AddHostedService<HealthCheckNotifier>();
|
||||
builder.Services.AddHostedService<KeepAlive>();
|
||||
builder.Services.AddHostedService<LogScrubber>();
|
||||
builder.Services.AddHostedService<ScheduledPublishing>();
|
||||
builder.Services.AddHostedService<TempFileCleanup>();
|
||||
builder.Services.AddHostedService<InstructionProcessTask>();
|
||||
builder.Services.AddHostedService<TouchServerTask>();
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static IUmbracoBuilder AddHttpClients(this IUmbracoBuilder builder)
|
||||
{
|
||||
builder.Services.AddUmbracoHttpClients();
|
||||
builder.Services.AddHttpClient();
|
||||
return builder;
|
||||
}
|
||||
|
||||
@@ -189,5 +286,76 @@ namespace Umbraco.Web.Common.Builder
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds SqlCe support for Umbraco
|
||||
/// </summary>
|
||||
private static void AddUmbracoSqlCeSupport(
|
||||
this IServiceCollection services,
|
||||
ICollection<ISqlSyntaxProvider> syntaxProviders,
|
||||
ICollection<IBulkSqlInsertProvider> insertProviders,
|
||||
ICollection<IEmbeddedDatabaseCreator> databaseCreators)
|
||||
{
|
||||
try
|
||||
{
|
||||
var binFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||
if (binFolder != null)
|
||||
{
|
||||
var dllPath = Path.Combine(binFolder, "Umbraco.Persistance.SqlCe.dll");
|
||||
var umbSqlCeAssembly = Assembly.LoadFrom(dllPath);
|
||||
|
||||
var sqlCeSyntaxProviderType = umbSqlCeAssembly.GetType("Umbraco.Persistance.SqlCe.SqlCeSyntaxProvider");
|
||||
var sqlCeBulkSqlInsertProviderType = umbSqlCeAssembly.GetType("Umbraco.Persistance.SqlCe.SqlCeBulkSqlInsertProvider");
|
||||
var sqlCeEmbeddedDatabaseCreatorType = umbSqlCeAssembly.GetType("Umbraco.Persistance.SqlCe.SqlCeEmbeddedDatabaseCreator");
|
||||
|
||||
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);
|
||||
|
||||
syntaxProviders.Add((ISqlSyntaxProvider)Activator.CreateInstance(sqlCeSyntaxProviderType));
|
||||
insertProviders.Add((IBulkSqlInsertProvider)Activator.CreateInstance(sqlCeBulkSqlInsertProviderType));
|
||||
databaseCreators.Add((IEmbeddedDatabaseCreator)Activator.CreateInstance(sqlCeEmbeddedDatabaseCreatorType));
|
||||
}
|
||||
|
||||
var sqlCeAssembly = Assembly.LoadFrom(Path.Combine(binFolder, "System.Data.SqlServerCe.dll"));
|
||||
|
||||
var sqlCe = sqlCeAssembly.GetType("System.Data.SqlServerCe.SqlCeProviderFactory");
|
||||
if (!(sqlCe is null))
|
||||
{
|
||||
DbProviderFactories.RegisterFactory(Core.Constants.DbProviderNames.SqlCe, sqlCe);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore if SqlCE is not available
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds Sql Server support for Umbraco
|
||||
/// </summary>
|
||||
private static void AddUmbracoSqlServerSupport(
|
||||
this IServiceCollection services,
|
||||
ICollection<ISqlSyntaxProvider> syntaxProviders,
|
||||
ICollection<IBulkSqlInsertProvider> insertProviders,
|
||||
ICollection<IEmbeddedDatabaseCreator> databaseCreators)
|
||||
{
|
||||
DbProviderFactories.RegisterFactory(Core.Constants.DbProviderNames.SqlServer, SqlClientFactory.Instance);
|
||||
|
||||
var syntaxProvider = new SqlServerSyntaxProvider();
|
||||
var insertProvider = new SqlServerBulkSqlInsertProvider();
|
||||
var databaseCreator = new NoopEmbeddedDatabaseCreator();
|
||||
|
||||
services.AddSingleton<ISqlSyntaxProvider>(syntaxProvider);
|
||||
services.AddSingleton<IBulkSqlInsertProvider>(insertProvider);
|
||||
services.AddSingleton<IEmbeddedDatabaseCreator>(databaseCreator);
|
||||
|
||||
syntaxProviders.Add(syntaxProvider);
|
||||
insertProviders.Add(insertProvider);
|
||||
databaseCreators.Add(databaseCreator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,274 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Data.SqlClient;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Serilog;
|
||||
using Serilog.Extensions.Hosting;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Builder;
|
||||
using Umbraco.Core.Cache;
|
||||
using Umbraco.Core.Composing;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Configuration.Models;
|
||||
using Umbraco.Core.Configuration.Models.Validation;
|
||||
using Umbraco.Core.IO;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Logging.Serilog;
|
||||
using Umbraco.Core.Persistence;
|
||||
using Umbraco.Core.Persistence.SqlSyntax;
|
||||
using Umbraco.Core.Runtime;
|
||||
using Umbraco.Infrastructure.HostedServices;
|
||||
using Umbraco.Infrastructure.HostedServices.ServerRegistration;
|
||||
using Umbraco.Web.Common.AspNetCore;
|
||||
using Umbraco.Web.Common.Profiler;
|
||||
using ConnectionStrings = Umbraco.Core.Configuration.Models.ConnectionStrings;
|
||||
using IHostingEnvironment = Umbraco.Core.Hosting.IHostingEnvironment;
|
||||
|
||||
namespace Umbraco.Extensions
|
||||
{
|
||||
public static class UmbracoCoreServiceCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds the Umbraco Back Core requirements
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <param name="webHostEnvironment"></param>
|
||||
/// <param name="entryAssembly"></param>
|
||||
/// <param name="appCaches"></param>
|
||||
/// <param name="loggingConfiguration"></param>
|
||||
/// <param name="configuration"></param>
|
||||
/// <returns></returns>
|
||||
public static IUmbracoBuilder AddUmbracoCore(
|
||||
this IUmbracoBuilder builder,
|
||||
IWebHostEnvironment webHostEnvironment,
|
||||
Assembly entryAssembly,
|
||||
ILoggingConfiguration loggingConfiguration,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
if (builder is null) throw new ArgumentNullException(nameof(builder));
|
||||
if (entryAssembly is null) throw new ArgumentNullException(nameof(entryAssembly));
|
||||
|
||||
builder.Services.AddLazySupport();
|
||||
|
||||
// Add service session
|
||||
// This can be overwritten by the user by adding their own call to AddSession
|
||||
// since the last call of AddSession take precedence
|
||||
builder.Services.AddSession(options =>
|
||||
{
|
||||
options.Cookie.Name = "UMB_SESSION";
|
||||
options.Cookie.HttpOnly = true;
|
||||
});
|
||||
var syntaxProviders = new List<ISqlSyntaxProvider>();
|
||||
var insertProviders = new List<IBulkSqlInsertProvider>();
|
||||
var databaseCreators = new List<IEmbeddedDatabaseCreator>();
|
||||
|
||||
// Add supported databases
|
||||
builder.Services.AddUmbracoSqlCeSupport(syntaxProviders, insertProviders, databaseCreators);
|
||||
builder.Services.AddUmbracoSqlServerSupport(syntaxProviders, insertProviders, databaseCreators);
|
||||
|
||||
builder.Services.AddUnique<IAppPolicyCache>(factory => factory.GetRequiredService<AppCaches>().RuntimeCache);
|
||||
builder.Services.AddUnique<IRequestCache>(factory => factory.GetRequiredService<AppCaches>().RequestCache);
|
||||
|
||||
var dbProviderFactoryCreator = new DbProviderFactoryCreator(
|
||||
DbProviderFactories.GetFactory,
|
||||
syntaxProviders,
|
||||
insertProviders,
|
||||
databaseCreators);
|
||||
|
||||
builder.Services.AddSingleton<IDbProviderFactoryCreator>(dbProviderFactoryCreator);
|
||||
|
||||
builder.Services.AddUnique<IIOHelper, IOHelper>();
|
||||
builder.Services.AddLogger(loggingConfiguration, configuration); // TODO: remove this line
|
||||
var loggerFactory = builder.BuilderLoggerFactory;
|
||||
|
||||
var profiler = GetWebProfiler(configuration);
|
||||
builder.Services.AddUnique<IProfiler>(profiler);
|
||||
|
||||
|
||||
var typeFinder = CreateTypeFinder(loggerFactory, webHostEnvironment, entryAssembly, builder.Config);
|
||||
|
||||
builder.Services.AddUnique<IProfilingLogger,ProfilingLogger>();
|
||||
builder.Services.AddUnique<ITypeFinder>(typeFinder);
|
||||
builder.Services.AddUnique<TypeLoader>(builder.TypeLoader);
|
||||
builder.Services.AddUnique<IBackOfficeInfo, AspNetCoreBackOfficeInfo>();
|
||||
|
||||
// after bootstrapping we let the container wire up for us.
|
||||
builder.Services.AddUnique<IUmbracoDatabaseFactory, UmbracoDatabaseFactory>();
|
||||
builder.Services.AddUnique<ISqlContext>(factory => factory.GetRequiredService<IUmbracoDatabaseFactory>().SqlContext);
|
||||
builder.Services.AddUnique<IBulkSqlInsertProvider>(factory => factory.GetRequiredService<IUmbracoDatabaseFactory>().BulkSqlInsertProvider);
|
||||
|
||||
builder.Services.AddUnique<IUmbracoVersion, UmbracoVersion>();
|
||||
|
||||
builder.Services.AddUnique<IDbProviderFactoryCreator>(dbProviderFactoryCreator);
|
||||
builder.Services.AddUnique<IRuntime, CoreRuntime>();
|
||||
builder.Services.AddUnique<IRuntimeState, RuntimeState>();
|
||||
builder.Services.AddUnique<IHostingEnvironment, AspNetCoreHostingEnvironment>();
|
||||
|
||||
builder.Services.AddUnique<IMainDomLock>(factory =>
|
||||
{
|
||||
var globalSettings = factory.GetRequiredService<IOptions<GlobalSettings>>().Value;
|
||||
var connectionStrings = factory.GetRequiredService<IOptions<ConnectionStrings>>().Value;
|
||||
var hostingEnvironment = factory.GetRequiredService<IHostingEnvironment>();
|
||||
|
||||
var dbCreator = factory.GetRequiredService<IDbProviderFactoryCreator>();
|
||||
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
||||
|
||||
return globalSettings.MainDomLock.Equals("SqlMainDomLock") || isWindows == false
|
||||
? (IMainDomLock)new SqlMainDomLock(builder.BuilderLoggerFactory.CreateLogger<SqlMainDomLock>(), builder.BuilderLoggerFactory, globalSettings, connectionStrings, dbCreator, hostingEnvironment)
|
||||
: new MainDomSemaphoreLock(builder.BuilderLoggerFactory.CreateLogger<MainDomSemaphoreLock>(), hostingEnvironment);
|
||||
});
|
||||
|
||||
builder.Services.AddUnique<IMainDom, MainDom>();
|
||||
|
||||
builder.Services.AddUnique<IUmbracoBootPermissionChecker>(new AspNetCoreBootPermissionsChecker());
|
||||
|
||||
builder.AddComposers();
|
||||
|
||||
var exceptionLogger = builder.BuilderLoggerFactory.CreateLogger<object>();
|
||||
AppDomain.CurrentDomain.UnhandledException += (_, args) =>
|
||||
{
|
||||
var exception = (Exception)args.ExceptionObject;
|
||||
var isTerminating = args.IsTerminating; // always true?
|
||||
|
||||
var msg = "Unhandled exception in AppDomain";
|
||||
if (isTerminating) msg += " (terminating)";
|
||||
msg += ".";
|
||||
exceptionLogger.LogError(exception, msg);
|
||||
};
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static IUmbracoBuilder AddComposers(this IUmbracoBuilder builder)
|
||||
{
|
||||
var composerTypes = builder.TypeLoader.GetTypes<IComposer>();
|
||||
var enableDisable = builder.TypeLoader.GetAssemblyAttributes(typeof(EnableComposerAttribute), typeof(DisableComposerAttribute));
|
||||
new Composers(builder, composerTypes, enableDisable, builder.BuilderLoggerFactory.CreateLogger<Composers>()).Compose();
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds SqlCe support for Umbraco
|
||||
/// </summary>
|
||||
private static IServiceCollection AddUmbracoSqlCeSupport(
|
||||
this IServiceCollection services,
|
||||
ICollection<ISqlSyntaxProvider> syntaxProviders,
|
||||
ICollection<IBulkSqlInsertProvider> insertProviders,
|
||||
ICollection<IEmbeddedDatabaseCreator> databaseCreators)
|
||||
{
|
||||
try
|
||||
{
|
||||
var binFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||
if (binFolder != null)
|
||||
{
|
||||
var dllPath = Path.Combine(binFolder, "Umbraco.Persistance.SqlCe.dll");
|
||||
var umbSqlCeAssembly = Assembly.LoadFrom(dllPath);
|
||||
|
||||
var sqlCeSyntaxProviderType = umbSqlCeAssembly.GetType("Umbraco.Persistance.SqlCe.SqlCeSyntaxProvider");
|
||||
var sqlCeBulkSqlInsertProviderType = umbSqlCeAssembly.GetType("Umbraco.Persistance.SqlCe.SqlCeBulkSqlInsertProvider");
|
||||
var sqlCeEmbeddedDatabaseCreatorType = umbSqlCeAssembly.GetType("Umbraco.Persistance.SqlCe.SqlCeEmbeddedDatabaseCreator");
|
||||
|
||||
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);
|
||||
|
||||
syntaxProviders.Add((ISqlSyntaxProvider)Activator.CreateInstance(sqlCeSyntaxProviderType));
|
||||
insertProviders.Add((IBulkSqlInsertProvider)Activator.CreateInstance(sqlCeBulkSqlInsertProviderType));
|
||||
databaseCreators.Add((IEmbeddedDatabaseCreator)Activator.CreateInstance(sqlCeEmbeddedDatabaseCreatorType));
|
||||
}
|
||||
|
||||
var sqlCeAssembly = Assembly.LoadFrom(Path.Combine(binFolder, "System.Data.SqlServerCe.dll"));
|
||||
|
||||
var sqlCe = sqlCeAssembly.GetType("System.Data.SqlServerCe.SqlCeProviderFactory");
|
||||
if (!(sqlCe is null))
|
||||
{
|
||||
DbProviderFactories.RegisterFactory(Core.Constants.DbProviderNames.SqlCe, sqlCe);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore if SqlCE is not available
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds Sql Server support for Umbraco
|
||||
/// </summary>
|
||||
public static IServiceCollection AddUmbracoSqlServerSupport(
|
||||
this IServiceCollection services,
|
||||
ICollection<ISqlSyntaxProvider> syntaxProviders,
|
||||
ICollection<IBulkSqlInsertProvider> insertProviders,
|
||||
ICollection<IEmbeddedDatabaseCreator> databaseCreators)
|
||||
{
|
||||
DbProviderFactories.RegisterFactory(Core.Constants.DbProviderNames.SqlServer, SqlClientFactory.Instance);
|
||||
|
||||
var syntaxProvider = new SqlServerSyntaxProvider();
|
||||
var insertProvider = new SqlServerBulkSqlInsertProvider();
|
||||
var databaseCreator = new NoopEmbeddedDatabaseCreator();
|
||||
|
||||
services.AddSingleton<ISqlSyntaxProvider>(syntaxProvider);
|
||||
services.AddSingleton<IBulkSqlInsertProvider>(insertProvider);
|
||||
services.AddSingleton<IEmbeddedDatabaseCreator>(databaseCreator);
|
||||
|
||||
syntaxProviders.Add(syntaxProvider);
|
||||
insertProviders.Add(insertProvider);
|
||||
databaseCreators.Add(databaseCreator);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds hosted services for Umbraco.
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddUmbracoHostedServices(this IServiceCollection services)
|
||||
{
|
||||
services.AddHostedService<HealthCheckNotifier>();
|
||||
services.AddHostedService<KeepAlive>();
|
||||
services.AddHostedService<LogScrubber>();
|
||||
services.AddHostedService<ScheduledPublishing>();
|
||||
services.AddHostedService<TempFileCleanup>();
|
||||
|
||||
services.AddHostedService<InstructionProcessTask>();
|
||||
services.AddHostedService<TouchServerTask>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds HTTP clients for Umbraco.
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddUmbracoHttpClients(this IServiceCollection services)
|
||||
{
|
||||
services.AddHttpClient();
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create and configure the logger
|
||||
/// </summary>
|
||||
/// <param name="hostingEnvironment"></param>
|
||||
public static IServiceCollection AddLogger(
|
||||
this IServiceCollection services,
|
||||
ILoggingConfiguration loggingConfiguration,
|
||||
@@ -280,7 +36,6 @@ namespace Umbraco.Extensions
|
||||
// This is nessasary to pick up all the loggins to MS ILogger.
|
||||
Log.Logger = logger.SerilogLog;
|
||||
|
||||
|
||||
// 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
|
||||
// is the only other option that these ext methods allow.
|
||||
@@ -307,7 +62,7 @@ namespace Umbraco.Extensions
|
||||
return services;
|
||||
}
|
||||
|
||||
private static IProfiler GetWebProfiler(IConfiguration config)
|
||||
internal static IProfiler GetWebProfiler(IConfiguration config)
|
||||
{
|
||||
var isDebug = config.GetValue<bool>($"{Constants.Configuration.ConfigHosting}:Debug");
|
||||
// create and start asap to profile boot
|
||||
@@ -324,7 +79,7 @@ namespace Umbraco.Extensions
|
||||
return webProfiler;
|
||||
}
|
||||
|
||||
internal static ITypeFinder CreateTypeFinder(ILoggerFactory loggerFactory, IWebHostEnvironment webHostEnvironment, Assembly entryAssembly, IConfiguration config)
|
||||
internal static ITypeFinder AddTypeFinder(this IServiceCollection services, ILoggerFactory loggerFactory, IWebHostEnvironment webHostEnvironment, Assembly entryAssembly, IConfiguration config)
|
||||
{
|
||||
var profiler = GetWebProfiler(config);
|
||||
|
||||
@@ -333,37 +88,46 @@ namespace Umbraco.Extensions
|
||||
var runtimeHashPaths = new RuntimeHashPaths().AddFolder(new DirectoryInfo(Path.Combine(webHostEnvironment.ContentRootPath, "bin")));
|
||||
var runtimeHash = new RuntimeHash(new ProfilingLogger(loggerFactory.CreateLogger<ProfilingLogger>(), profiler), runtimeHashPaths);
|
||||
|
||||
return new TypeFinder(
|
||||
var typeFinder = new TypeFinder(
|
||||
loggerFactory.CreateLogger<TypeFinder>(),
|
||||
new DefaultUmbracoAssemblyProvider(entryAssembly),
|
||||
runtimeHash,
|
||||
new TypeFinderConfig(Options.Create(typeFinderSettings))
|
||||
);
|
||||
|
||||
services.AddUnique<ITypeFinder>(typeFinder);
|
||||
|
||||
return typeFinder;
|
||||
}
|
||||
|
||||
internal static TypeLoader CreateTypeLoader(
|
||||
internal static TypeLoader AddTypeLoader(
|
||||
this IServiceCollection services,
|
||||
Assembly entryAssembly,
|
||||
IWebHostEnvironment webHostEnvironment,
|
||||
ILoggerFactory loggerFactory,
|
||||
AppCaches appCaches,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
var typeFinder = CreateTypeFinder(loggerFactory, webHostEnvironment, entryAssembly, configuration);
|
||||
var typeFinder = services.AddTypeFinder(loggerFactory, webHostEnvironment, entryAssembly, configuration);
|
||||
var hostingSettings = configuration.GetSection(Core.Constants.Configuration.ConfigHosting).Get<HostingSettings>() ?? new HostingSettings();
|
||||
var hostingEnvironment = new AspNetCoreHostingEnvironmentWithoutOptionsMonitor(hostingSettings, webHostEnvironment);
|
||||
|
||||
var profilingLogger = new ProfilingLogger(loggerFactory.CreateLogger<ProfilingLogger>(), GetWebProfiler(configuration));
|
||||
|
||||
return new TypeLoader(
|
||||
var typeLoader = new TypeLoader(
|
||||
typeFinder,
|
||||
appCaches.RuntimeCache,
|
||||
new DirectoryInfo(hostingEnvironment.LocalTempPath),
|
||||
loggerFactory.CreateLogger<TypeLoader>(),
|
||||
profilingLogger
|
||||
);
|
||||
|
||||
services.AddUnique<TypeLoader>(typeLoader);
|
||||
|
||||
return typeLoader;
|
||||
}
|
||||
|
||||
private class AspNetCoreBootPermissionsChecker : IUmbracoBootPermissionChecker
|
||||
internal class AspNetCoreBootPermissionsChecker : IUmbracoBootPermissionChecker
|
||||
{
|
||||
public void ThrowIfNotPermissions()
|
||||
{
|
||||
|
||||
@@ -32,12 +32,10 @@ namespace Umbraco.Web.UI.NetCore
|
||||
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
var umbracoBuilder = services.AddUmbraco(_env, _config);
|
||||
umbracoBuilder
|
||||
.AddAllBackOfficeComponents(_env)
|
||||
services.AddUmbraco(_env, _config)
|
||||
.AddAllBackOfficeComponents()
|
||||
.AddUmbracoWebsite()
|
||||
.Build();
|
||||
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
||||
Reference in New Issue
Block a user