Files
Umbraco-CMS/src/Umbraco.Web.BackOffice/AspNetCore/UmbracoBackOfficeServiceCollectionExtensions.cs

81 lines
3.1 KiB
C#
Raw Normal View History

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.Hosting;
2020-02-25 08:56:58 +01:00
using Umbraco.Composing;
2020-03-16 14:02:08 +01:00
using Umbraco.Configuration;
2020-03-16 19:14:04 +01:00
using Umbraco.Core;
2020-02-24 16:18:47 +01:00
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Logging.Serilog;
2020-02-18 08:32:06 +01:00
namespace Umbraco.Web.BackOffice.AspNetCore
{
public static class UmbracoBackOfficeServiceCollectionExtensions
{
2020-03-17 17:56:00 +01:00
public static IServiceCollection AddUmbracoConfiguration(this IServiceCollection services)
{
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetService<IConfiguration>();
var configsFactory = new AspNetCoreConfigsFactory(configuration);
var configs = configsFactory.Create();
services.AddSingleton(configs);
return services;
}
2020-02-18 08:32:06 +01:00
public static IServiceCollection AddUmbracoBackOffice(this IServiceCollection services)
{
2020-03-16 14:02:08 +01:00
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
2020-02-24 16:18:47 +01:00
2020-03-16 14:02:08 +01:00
var serviceProvider = services.BuildServiceProvider();
var httpContextAccessor = serviceProvider.GetService<IHttpContextAccessor>();
var webHostEnvironment = serviceProvider.GetService<IWebHostEnvironment>();
var hostApplicationLifetime = serviceProvider.GetService<IHostApplicationLifetime>();
2020-03-17 17:56:00 +01:00
var configs = serviceProvider.GetService<Configs>();
2020-03-16 14:02:08 +01:00
services.CreateCompositionRoot(
httpContextAccessor,
webHostEnvironment,
hostApplicationLifetime,
2020-03-17 17:56:00 +01:00
configs);
2020-02-24 16:18:47 +01:00
2020-02-18 08:32:06 +01:00
return services;
}
2020-02-25 08:56:58 +01:00
2020-03-16 14:02:08 +01:00
public static IServiceCollection CreateCompositionRoot(
this IServiceCollection services,
IHttpContextAccessor httpContextAccessor,
IWebHostEnvironment webHostEnvironment,
IHostApplicationLifetime hostApplicationLifetime,
2020-03-17 17:56:00 +01:00
Configs configs)
2020-02-24 16:18:47 +01:00
{
2020-03-16 19:14:04 +01:00
var hostingSettings = configs.Hosting();
var coreDebug = configs.CoreDebug();
var globalSettings = configs.Global();
2020-02-24 16:18:47 +01:00
var hostingEnvironment = new AspNetCoreHostingEnvironment(hostingSettings, webHostEnvironment,
httpContextAccessor, hostApplicationLifetime);
var ioHelper = new IOHelper(hostingEnvironment, globalSettings);
var logger = SerilogLogger.CreateWithDefaultConfiguration(hostingEnvironment,
new AspNetCoreSessionIdResolver(httpContextAccessor),
() => services.BuildServiceProvider().GetService<IRequestCache>(), coreDebug, ioHelper,
new AspNetCoreMarchal());
var backOfficeInfo = new AspNetCoreBackOfficeInfo(globalSettings);
2020-02-24 16:18:47 +01:00
var profiler = new LogProfiler(logger);
2020-02-25 08:56:58 +01:00
Current.Initialize(logger, configs, ioHelper, hostingEnvironment, backOfficeInfo, profiler);
2020-03-16 14:02:08 +01:00
return services;
2020-02-24 16:18:47 +01:00
}
2020-02-18 08:32:06 +01:00
}
}