Files
Umbraco-CMS/src/Umbraco.Web.Common/Extensions/UmbracoCoreServiceCollectionExtensions.cs

344 lines
15 KiB
C#
Raw Normal View History

using System;
using System.Data.Common;
2020-04-27 10:09:10 +02:00
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
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;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Serilog;
using Serilog.Extensions.Hosting;
2020-03-16 19:14:04 +01:00
using Umbraco.Core;
using Umbraco.Core.Builder;
2020-02-24 16:18:47 +01:00
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
2020-02-24 16:18:47 +01:00
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.Models;
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;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.SqlSyntax;
2020-02-25 08:56:58 +01:00
using Umbraco.Core.Runtime;
using Umbraco.Infrastructure.HostedServices;
using Umbraco.Infrastructure.HostedServices.ServerRegistration;
2020-03-31 12:22:11 +02:00
using Umbraco.Web.Common.AspNetCore;
using Umbraco.Web.Common.Profiler;
using ConnectionStrings = Umbraco.Core.Configuration.Models.ConnectionStrings;
2020-09-07 15:28:58 +02:00
using IHostingEnvironment = Umbraco.Core.Hosting.IHostingEnvironment;
2020-02-18 08:32:06 +01:00
namespace Umbraco.Extensions
2020-02-18 08:32:06 +01:00
{
public static class UmbracoCoreServiceCollectionExtensions
2020-02-18 08:32:06 +01:00
{
2020-11-19 09:06:04 +00:00
2020-03-13 19:10:21 +11: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>
2020-09-07 15:28:58 +02:00
/// <param name="requestCache"></param>
/// <param name="httpContextAccessor"></param>
/// <param name="loggingConfiguration"></param>
/// <param name="getRuntimeBootstrapper">Delegate to create an <see cref="CoreRuntimeBootstrapper"/></param>
/// <param name="factory"></param>
/// <returns></returns>
public static IUmbracoBuilder AddUmbracoCore(
this IUmbracoBuilder builder,
IWebHostEnvironment webHostEnvironment,
Assembly entryAssembly,
2020-09-07 15:28:58 +02:00
AppCaches appCaches,
ILoggingConfiguration loggingConfiguration,
IConfiguration configuration,
2020-09-07 15:28:58 +02:00
//TODO: Yep that's extremely ugly
Func<GlobalSettings, ConnectionStrings, IUmbracoVersion, IIOHelper, ILoggerFactory, IProfiler, IHostingEnvironment, IBackOfficeInfo, ITypeFinder, AppCaches, IDbProviderFactoryCreator, CoreRuntimeBootstrapper> getRuntimeBootstrapper)
2020-02-18 08:32:06 +01:00
{
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;
});
// Add supported databases
builder.Services.AddUmbracoSqlCeSupport();
builder.Services.AddUmbracoSqlServerSupport();
builder.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`.
var serviceProvider = builder.Services.BuildServiceProvider();
2020-09-15 15:14:44 +02:00
var globalSettings = serviceProvider.GetService<IOptionsMonitor<GlobalSettings>>();
var connectionStrings = serviceProvider.GetService<IOptions<ConnectionStrings>>();
var hostingSettings = serviceProvider.GetService<IOptionsMonitor<HostingSettings>>();
var typeFinderSettings = serviceProvider.GetService<IOptionsMonitor<TypeFinderSettings>>();
2020-04-29 08:56:42 +02:00
var dbProviderFactoryCreator = serviceProvider.GetRequiredService<IDbProviderFactoryCreator>();
2020-02-24 16:18:47 +01:00
var hostingEnvironment = new AspNetCoreHostingEnvironment(hostingSettings, webHostEnvironment);
var ioHelper = new IOHelper(hostingEnvironment);
var backOfficeInfo = new AspNetCoreBackOfficeInfo(globalSettings);
var profiler = GetWebProfiler(hostingEnvironment);
builder.Services.AddLogger(loggingConfiguration, configuration);
var loggerFactory = builder.Services.BuildServiceProvider().GetService<ILoggerFactory>();
2020-03-13 19:10:21 +11:00
var umbracoVersion = new UmbracoVersion();
var typeFinder = CreateTypeFinder(loggerFactory, profiler, webHostEnvironment, entryAssembly, typeFinderSettings);
var bootstrapper = getRuntimeBootstrapper(
globalSettings.CurrentValue,
connectionStrings.Value,
umbracoVersion,
ioHelper,
2020-09-15 15:14:44 +02:00
loggerFactory,
profiler,
hostingEnvironment,
backOfficeInfo,
2020-04-27 10:09:10 +02:00
typeFinder,
appCaches,
2020-04-28 07:01:30 +02:00
dbProviderFactoryCreator);
bootstrapper.Configure(builder);
builder.AddComposers();
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>
/// <param name="services"></param>
/// <returns></returns>
private 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");
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);
}
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>
/// <param name="services"></param>
/// <returns></returns>
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;
}
/// <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;
}
2020-09-18 12:53:06 +02:00
private static ITypeFinder CreateTypeFinder(ILoggerFactory loggerFactory, IProfiler profiler, IWebHostEnvironment webHostEnvironment, Assembly entryAssembly, IOptionsMonitor<TypeFinderSettings> typeFinderSettings)
{
var runtimeHashPaths = new RuntimeHashPaths();
runtimeHashPaths.AddFolder(new DirectoryInfo(Path.Combine(webHostEnvironment.ContentRootPath, "bin")));
var runtimeHash = new RuntimeHash(new ProfilingLogger(loggerFactory.CreateLogger("RuntimeHash"), profiler), runtimeHashPaths);
return new TypeFinder(loggerFactory.CreateLogger<TypeFinder>(), new DefaultUmbracoAssemblyProvider(entryAssembly), runtimeHash, new TypeFinderConfig(typeFinderSettings));
}
internal static CoreRuntimeBootstrapper GetCoreRuntime(
GlobalSettings globalSettings, ConnectionStrings connectionStrings, IUmbracoVersion umbracoVersion, IIOHelper ioHelper, ILoggerFactory loggerFactory,
2020-09-08 13:03:43 +02:00
IProfiler profiler, IHostingEnvironment hostingEnvironment, IBackOfficeInfo backOfficeInfo,
ITypeFinder typeFinder, AppCaches appCaches, IDbProviderFactoryCreator dbProviderFactoryCreator)
{
// Determine if we should use the sql main dom or the default
var appSettingMainDomLock = globalSettings.MainDomLock;
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)
: new MainDomSemaphoreLock(loggerFactory.CreateLogger<MainDomSemaphoreLock>(), hostingEnvironment);
var mainDom = new MainDom(loggerFactory.CreateLogger<MainDom>(), mainDomLock);
var coreRuntime = new CoreRuntimeBootstrapper(
globalSettings,
connectionStrings,
umbracoVersion,
ioHelper,
profiler,
new AspNetCoreBootPermissionsChecker(),
hostingEnvironment,
backOfficeInfo,
dbProviderFactoryCreator,
mainDom,
typeFinder,
appCaches);
return coreRuntime;
}
2020-02-25 08:56:58 +01:00
2020-03-24 10:51:53 +01:00
/// <summary>
/// Create and configure the logger
/// </summary>
/// <param name="hostingEnvironment"></param>
public static IServiceCollection AddLogger(
this IServiceCollection services,
ILoggingConfiguration loggingConfiguration,
IConfiguration configuration)
{
// Create a serilog logger
var logger = SerilogLogger.CreateWithDefaultConfiguration(loggingConfiguration, configuration);
// 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.
// 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-17 09:59:13 +02:00
configure.AddSerilog(logger.SerilogLog, false);
});
// This won't (and shouldn't) take ownership of the logger.
services.AddSingleton(logger.SerilogLog);
// Registered to provide two services...
var diagnosticContext = new DiagnosticContext(logger.SerilogLog);
// Consumed by e.g. middleware
services.AddSingleton(diagnosticContext);
// Consumed by user code
services.AddSingleton<IDiagnosticContext>(diagnosticContext);
return services;
}
private static IProfiler GetWebProfiler(Umbraco.Core.Hosting.IHostingEnvironment hostingEnvironment)
{
// 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();
}
var webProfiler = new WebProfiler();
webProfiler.StartBoot();
return webProfiler;
}
private class AspNetCoreBootPermissionsChecker : IUmbracoBootPermissionChecker
{
public void ThrowIfNotPermissions()
{
// nothing to check
}
}
2020-02-18 08:32:06 +01:00
}
2020-02-18 08:32:06 +01:00
}