Creating composition root

This commit is contained in:
Bjarke Berg
2020-02-24 16:18:47 +01:00
parent 20201fb6be
commit 36b68f2966
9 changed files with 146 additions and 9 deletions
@@ -56,8 +56,8 @@ namespace Umbraco.Tests.Routing
public class TestRuntime : WebRuntime
{
public TestRuntime(UmbracoApplicationBase umbracoApplication, Configs configs, IUmbracoVersion umbracoVersion, IIOHelper ioHelper, ILogger logger, IHostingEnvironment hostingEnvironment, IBackOfficeInfo backOfficeInfo)
: base(umbracoApplication, configs, umbracoVersion, ioHelper, Mock.Of<ILogger>(), Mock.Of<IProfiler>(), hostingEnvironment, backOfficeInfo, TestHelper.DbProviderFactoryCreator, TestHelper.MainDom)
public TestRuntime(Configs configs, IUmbracoVersion umbracoVersion, IIOHelper ioHelper, ILogger logger, IHostingEnvironment hostingEnvironment, IBackOfficeInfo backOfficeInfo)
: base(configs, umbracoVersion, ioHelper, Mock.Of<ILogger>(), Mock.Of<IProfiler>(), hostingEnvironment, backOfficeInfo, TestHelper.DbProviderFactoryCreator, TestHelper.MainDom)
{
}
+74
View File
@@ -0,0 +1,74 @@
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="Umbraco.Web" />
<add namespace="Umbraco.Core" />
<add namespace="Umbraco.Core.Models" />
<add namespace="Umbraco.Core.Models.PublishedContent" />
<add namespace="Umbraco.Web.Mvc" />
<add namespace="Examine" />
<add namespace="Umbraco.Web.PublishedModels" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
<system.web>
<compilation targetFramework="4.7.2">
<assemblies>
<add assembly="System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<!--
Views are compiled by the Microsoft.CodeDom.Providers.DotNetCompilerPlatform which uses the Roslyn compiler
from ~/bin/roslyn to expose an ICodeProvider implementation to System.CodeDom.
For instance, starting with package 1.4.0 (which contains version 1.2.2.0) System.Collections.Immutable
lists netstandard2.0 as a supported target, whereas package 1.3.1 only listed net45. So now, when
installing, NuGet picks the netstandard2.0 version, thus introducing a dependency to netstandard2.0.
Somehow, transitive dependencies are not working correctly, and either (a) NuGet fails to properly
register this dependency, or (b) when reference assemblies are gathered before compiling views, this
dependency is missed. In any case, the result is that the ICodeProvider is passed a list of referenced
assemblies that is missing .NET Standard.
It may be a mix of both. NuGet registers the dependency well enough, that we can actually build the
whole application - but it is not doing something that is required in order to have .NET Standard
being a dependency when building views.
See also: https://stackoverflow.com/questions/50165910
Funny enough, the CSharpCompiler already explicitly adds System.Runtime as a referenced assembly,
with a comment mentioning an issue. But it's not adding .NET Standard. So, for the time being, to be sure,
we are adding both here.
-->
<add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
</assemblies>
</compilation>
</system.web>
</configuration>
@@ -0,0 +1,16 @@
using Umbraco.Core;
using Umbraco.Core.Configuration;
namespace Umbraco.Web.BackOffice.AspNetCore
{
public class AspNetCoreBackOfficeInfo : IBackOfficeInfo
{
public AspNetCoreBackOfficeInfo(IGlobalSettings globalSettings)
{
GetAbsoluteUrl = globalSettings.UmbracoPath;
}
public string GetAbsoluteUrl { get; }
}
}
@@ -0,0 +1,13 @@
using System;
using System.Runtime.InteropServices;
using Umbraco.Core.Diagnostics;
namespace Umbraco.Web.BackOffice
{
public class AspNetCoreMarchal : IMarchal
{
// This thing is not available in net standard, but exists in both .Net 4 and .Net Core 3
public IntPtr GetExceptionPointers() => Marshal.GetExceptionPointers();
}
}
@@ -3,11 +3,11 @@ using Umbraco.Net;
namespace Umbraco.Web.BackOffice.AspNetCore
{
internal class AspNetSessionIdResolver : ISessionIdResolver
internal class AspNetCoreSessionIdResolver : ISessionIdResolver
{
private readonly IHttpContextAccessor _httpContextAccessor;
public AspNetSessionIdResolver(IHttpContextAccessor httpContextAccessor)
public AspNetCoreSessionIdResolver(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
@@ -1,4 +1,12 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Logging.Serilog;
namespace Umbraco.Web.BackOffice.AspNetCore
{
@@ -6,8 +14,37 @@ namespace Umbraco.Web.BackOffice.AspNetCore
{
public static IServiceCollection AddUmbracoBackOffice(this IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
CreateCompositionRoot(services);
return services;
}
private static void CreateCompositionRoot(IServiceCollection services)
{
var serviceProvider = services.BuildServiceProvider();
var httpContextAccessor = serviceProvider.GetService<IHttpContextAccessor>();
var webHostEnvironment = serviceProvider.GetService<IWebHostEnvironment>();
var configFactory = new ConfigsFactory();
var hostingSettings = configFactory.HostingSettings;
var coreDebug = configFactory.CoreDebug;
var hostingEnvironment = new AspNetCoreHostingEnvironment(hostingSettings, webHostEnvironment);
var ioHelper = new IOHelper(hostingEnvironment);
var configs = configFactory.Create(ioHelper);
var logger = SerilogLogger.CreateWithDefaultConfiguration(hostingEnvironment, new AspNetCoreSessionIdResolver(httpContextAccessor), () => services.BuildServiceProvider().GetService<IRequestCache>(), coreDebug, ioHelper, new AspNetCoreMarchal());
var backOfficeInfo = new AspNetCoreBackOfficeInfo(configs.Global());
var profiler = new LogProfiler(logger);
Composing.Current.Initialize(logger, configs, ioHelper, hostingEnvironment, backOfficeInfo, profiler);
}
}
}
@@ -11,6 +11,7 @@
<ItemGroup>
<ProjectReference Include="..\Umbraco.Abstractions\Umbraco.Abstractions.csproj" />
<ProjectReference Include="..\Umbraco.Configuration\Umbraco.Configuration.csproj" />
<ProjectReference Include="..\Umbraco.Core\Umbraco.Core.csproj" />
<ProjectReference Include="..\Umbraco.Infrastructure\Umbraco.Infrastructure.csproj" />
</ItemGroup>
-4
View File
@@ -22,15 +22,12 @@ namespace Umbraco.Web.Runtime
/// <remarks>On top of CoreRuntime, handles all of the web-related runtime aspects of Umbraco.</remarks>
public class WebRuntime : CoreRuntime
{
private readonly UmbracoApplicationBase _umbracoApplication;
private BuildManagerTypeFinder _typeFinder;
/// <summary>
/// Initializes a new instance of the <see cref="WebRuntime"/> class.
/// </summary>
/// <param name="umbracoApplication"></param>
public WebRuntime(
UmbracoApplicationBase umbracoApplication,
Configs configs,
IUmbracoVersion umbracoVersion,
IIOHelper ioHelper,
@@ -42,7 +39,6 @@ namespace Umbraco.Web.Runtime
IMainDom mainDom):
base(configs, umbracoVersion, ioHelper, logger, profiler ,new AspNetUmbracoBootPermissionChecker(), hostingEnvironment, backOfficeInfo, dbProviderFactoryCreator, mainDom)
{
_umbracoApplication = umbracoApplication;
Profiler = GetWebProfiler();
}
+1 -1
View File
@@ -34,7 +34,7 @@ namespace Umbraco.Web
var mainDom = new MainDom(logger, hostingEnvironment, mainDomLock);
return new WebRuntime(this, configs, umbracoVersion, ioHelper, logger, profiler, hostingEnvironment, backOfficeInfo, dbProviderFactoryCreator, mainDom);
return new WebRuntime(configs, umbracoVersion, ioHelper, logger, profiler, hostingEnvironment, backOfficeInfo, dbProviderFactoryCreator, mainDom);
}
/// <summary>