AB#5819 - Moved composing of core/infrastructure items into CoreInitialComposer
This commit is contained in:
@@ -31,5 +31,20 @@ namespace Umbraco.Core
|
||||
/// </summary>
|
||||
public static void RegisterUnique<TService>(this Composition composition, TService instance)
|
||||
=> composition.RegisterUnique(typeof(TService), instance);
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Registers a unique service with an implementation type.
|
||||
/// </summary>
|
||||
public static void RegisterMultipleUnique<TService1, TService2, TImplementing>(this Composition composition)
|
||||
where TImplementing : class, TService1, TService2
|
||||
where TService1 : class
|
||||
where TService2 : class
|
||||
{
|
||||
composition.RegisterUnique<TImplementing>();
|
||||
composition.RegisterUnique<TService1>(factory => factory.GetInstance<TImplementing>());
|
||||
composition.RegisterUnique<TService2>(factory => factory.GetInstance<TImplementing>());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ namespace Umbraco.Core.Session
|
||||
{
|
||||
public interface ISessionManager
|
||||
{
|
||||
object GetSessionValue(string sessionName);
|
||||
void SetSessionValue(string sessionName, object value);
|
||||
string GetSessionValue(string sessionName);
|
||||
void SetSessionValue(string sessionName, string value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Umbraco.Core.Cookie;
|
||||
|
||||
namespace Umbraco.Web.Common.AspNetCore
|
||||
{
|
||||
public class AspNetCoreCookieManager : ICookieManager
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public AspNetCoreCookieManager(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
public void ExpireCookie(string cookieName)
|
||||
{
|
||||
var httpContext = _httpContextAccessor.HttpContext;
|
||||
|
||||
if (httpContext is null) return;
|
||||
|
||||
var cookieValue = httpContext.Request.Cookies[cookieName];
|
||||
|
||||
httpContext.Response.Cookies.Append(cookieName, cookieValue, new CookieOptions()
|
||||
{
|
||||
Expires = DateTime.Now.AddYears(-1)
|
||||
});
|
||||
}
|
||||
|
||||
public string GetCookieValue(string cookieName)
|
||||
{
|
||||
return _httpContextAccessor.HttpContext?.Request.Cookies[cookieName];
|
||||
}
|
||||
|
||||
public void SetCookieValue(string cookieName, string value)
|
||||
{
|
||||
_httpContextAccessor.HttpContext?.Response.Cookies.Append(cookieName, value);
|
||||
}
|
||||
|
||||
public bool HasCookie(string cookieName)
|
||||
{
|
||||
return !(GetCookieValue(cookieName) is null);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Umbraco.Net;
|
||||
|
||||
namespace Umbraco.Web.Common.AspNetCore
|
||||
{
|
||||
internal class AspNetCoreSessionIdResolver : ISessionIdResolver
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public AspNetCoreSessionIdResolver(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
|
||||
public string SessionId
|
||||
{
|
||||
get
|
||||
{
|
||||
var httpContext = _httpContextAccessor?.HttpContext;
|
||||
// If session isn't enabled this will throw an exception so we check
|
||||
var sessionFeature = httpContext?.Features.Get<ISessionFeature>();
|
||||
return sessionFeature != null
|
||||
? httpContext?.Session?.Id
|
||||
: "0";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Umbraco.Core.Session;
|
||||
using Umbraco.Net;
|
||||
|
||||
namespace Umbraco.Web.Common.AspNetCore
|
||||
{
|
||||
internal class AspNetCoreSessionManager : ISessionIdResolver, ISessionManager
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public AspNetCoreSessionManager(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// If session isn't enabled this will throw an exception so we check
|
||||
/// </summary>
|
||||
private bool IsSessionsAvailable => !(_httpContextAccessor.HttpContext?.Features.Get<ISessionFeature>() is null);
|
||||
|
||||
public string SessionId
|
||||
{
|
||||
get
|
||||
{
|
||||
var httpContext = _httpContextAccessor?.HttpContext;
|
||||
|
||||
return IsSessionsAvailable
|
||||
? httpContext?.Session?.Id
|
||||
: "0";
|
||||
}
|
||||
}
|
||||
|
||||
public string GetSessionValue(string sessionName)
|
||||
{
|
||||
if(!IsSessionsAvailable) return null;
|
||||
return _httpContextAccessor.HttpContext?.Session.GetString(sessionName);
|
||||
}
|
||||
|
||||
|
||||
public void SetSessionValue(string sessionName, string value)
|
||||
{
|
||||
if(!IsSessionsAvailable) return;
|
||||
_httpContextAccessor.HttpContext?.Session.SetString(sessionName, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -164,7 +164,7 @@ namespace Umbraco.Web.Common.Extensions
|
||||
hostingEnvironment = new AspNetCoreHostingEnvironment(hostingSettings, webHostEnvironment, httpContextAccessor);
|
||||
ioHelper = new IOHelper(hostingEnvironment, globalSettings);
|
||||
logger = SerilogLogger.CreateWithDefaultConfiguration(hostingEnvironment,
|
||||
new AspNetCoreSessionIdResolver(httpContextAccessor),
|
||||
new AspNetCoreSessionManager(httpContextAccessor),
|
||||
// TODO: We need to avoid this, surely there's a way? See ContainerTests.BuildServiceProvider_Before_Host_Is_Configured
|
||||
() => services.BuildServiceProvider().GetService<IRequestCache>(), coreDebug, ioHelper,
|
||||
new AspNetCoreMarchal());
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Composing;
|
||||
using Umbraco.Core.Cookie;
|
||||
using Umbraco.Core.Hosting;
|
||||
using Umbraco.Core.Media;
|
||||
using Umbraco.Core.Models.PublishedContent;
|
||||
using Umbraco.Net;
|
||||
using Umbraco.Core.Runtime;
|
||||
using Umbraco.Core.Security;
|
||||
using Umbraco.Core.Session;
|
||||
using Umbraco.Infrastructure.Media;
|
||||
using Umbraco.Web.Common.AspNetCore;
|
||||
using Umbraco.Web.Common.Lifetime;
|
||||
@@ -39,15 +41,18 @@ namespace Umbraco.Web.Common.Runtime
|
||||
composition.RegisterUnique<IApplicationShutdownRegistry, AspNetCoreApplicationShutdownRegistry>();
|
||||
|
||||
// The umbraco request lifetime
|
||||
composition.RegisterUnique<UmbracoRequestLifetime>();
|
||||
composition.RegisterUnique<IUmbracoRequestLifetimeManager>(factory => factory.GetInstance<UmbracoRequestLifetime>());
|
||||
composition.RegisterUnique<IUmbracoRequestLifetime>(factory => factory.GetInstance<UmbracoRequestLifetime>());
|
||||
composition.RegisterMultipleUnique<IUmbracoRequestLifetime, IUmbracoRequestLifetimeManager, UmbracoRequestLifetime>();
|
||||
|
||||
|
||||
//Password hasher
|
||||
composition.RegisterUnique<IPasswordHasher, AspNetCorePasswordHasher>();
|
||||
|
||||
|
||||
composition.RegisterUnique<ICookieManager, AspNetCoreCookieManager>();
|
||||
|
||||
composition.RegisterMultipleUnique<ISessionIdResolver, ISessionManager, AspNetCoreSessionManager>();
|
||||
|
||||
composition.RegisterUnique<ISessionIdResolver, AspNetCoreSessionManager>();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,13 +11,15 @@ namespace Umbraco.Web.AspNet
|
||||
{
|
||||
}
|
||||
|
||||
public object GetSessionValue(string sessionName)
|
||||
public string GetSessionValue(string sessionName)
|
||||
{
|
||||
return HttpContext.Current.Session[sessionName];
|
||||
return HttpContext.Current?.Session[sessionName]?.ToString();
|
||||
}
|
||||
|
||||
public void SetSessionValue(string sessionName, object value)
|
||||
public void SetSessionValue(string sessionName, string value)
|
||||
{
|
||||
var httpContext = HttpContext.Current;
|
||||
if (httpContext is null) return;
|
||||
HttpContext.Current.Session[sessionName] = value;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace Umbraco.Web.Composing.CompositionExtensions
|
||||
.Add<TemplateMapDefinition>()
|
||||
.Add<UserMapDefinition>()
|
||||
.Add<LanguageMapDefinition>()
|
||||
.Add<IdentityMapDefinition>();;
|
||||
.Add<IdentityMapDefinition>();
|
||||
|
||||
composition.Register<CommonMapper>();
|
||||
composition.Register<CommonTreeNodeMapper>();
|
||||
|
||||
@@ -403,7 +403,7 @@ namespace Umbraco.Web.Macros
|
||||
attributeValue = _requestAccessor.GetRequestValue(name);
|
||||
break;
|
||||
case '%':
|
||||
attributeValue = _sessionManager.GetSessionValue(name)?.ToString();
|
||||
attributeValue = _sessionManager.GetSessionValue(name);
|
||||
if (string.IsNullOrEmpty(attributeValue))
|
||||
attributeValue = _cookieManager.GetCookieValue(name);
|
||||
break;
|
||||
|
||||
@@ -3,53 +3,31 @@ using System.Web.Security;
|
||||
using Examine;
|
||||
using Microsoft.AspNet.SignalR;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Cache;
|
||||
using Umbraco.Core.Composing;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Cookie;
|
||||
using Umbraco.Core.Dictionary;
|
||||
using Umbraco.Core.Events;
|
||||
using Umbraco.Core.Hosting;
|
||||
using Umbraco.Core.Install;
|
||||
using Umbraco.Core.Migrations.PostMigrations;
|
||||
using Umbraco.Core.Models.PublishedContent;
|
||||
using Umbraco.Core.PropertyEditors.ValueConverters;
|
||||
using Umbraco.Core.Runtime;
|
||||
using Umbraco.Core.Security;
|
||||
using Umbraco.Core.Services;
|
||||
using Umbraco.Core.Sync;
|
||||
using Umbraco.Web.Actions;
|
||||
using Umbraco.Web.Cache;
|
||||
using Umbraco.Web.Composing.CompositionExtensions;
|
||||
using Umbraco.Web.ContentApps;
|
||||
using Umbraco.Web.Editors;
|
||||
using Umbraco.Web.Features;
|
||||
using Umbraco.Web.HealthCheck;
|
||||
using Umbraco.Web.Hosting;
|
||||
using Umbraco.Web.Install;
|
||||
using Umbraco.Web.Macros;
|
||||
using Umbraco.Web.Media.EmbedProviders;
|
||||
using Umbraco.Web.Models.PublishedContent;
|
||||
using Umbraco.Web.Mvc;
|
||||
using Umbraco.Web.PublishedCache;
|
||||
using Umbraco.Web.Routing;
|
||||
using Umbraco.Web.Search;
|
||||
using Umbraco.Web.Sections;
|
||||
using Umbraco.Web.Security;
|
||||
using Umbraco.Web.Security.Providers;
|
||||
using Umbraco.Web.Services;
|
||||
using Umbraco.Web.SignalR;
|
||||
using Umbraco.Web.Templates;
|
||||
using Umbraco.Web.Trees;
|
||||
using Umbraco.Web.WebApi;
|
||||
using Umbraco.Web.PropertyEditors;
|
||||
using Umbraco.Examine;
|
||||
using Umbraco.Net;
|
||||
using Umbraco.Core.Request;
|
||||
using Umbraco.Core.Session;
|
||||
using Umbraco.Web.AspNet;
|
||||
using Umbraco.Core.Media;
|
||||
using Umbraco.Infrastructure.Media;
|
||||
|
||||
namespace Umbraco.Web.Runtime
|
||||
{
|
||||
@@ -66,9 +44,7 @@ namespace Umbraco.Web.Runtime
|
||||
composition.Register<IIpResolver, AspNetIpResolver>();
|
||||
|
||||
composition.Register<IUserAgentProvider, AspNetUserAgentProvider>();
|
||||
composition.Register<AspNetSessionManager>(Lifetime.Singleton);
|
||||
composition.Register<ISessionIdResolver>(factory => factory.GetInstance<AspNetSessionManager>(), Lifetime.Singleton);
|
||||
composition.Register<ISessionManager>(factory => factory.GetInstance<AspNetSessionManager>(), Lifetime.Singleton);
|
||||
|
||||
|
||||
composition.Register<IRequestAccessor, AspNetRequestAccessor>(Lifetime.Singleton);
|
||||
|
||||
@@ -76,8 +52,7 @@ namespace Umbraco.Web.Runtime
|
||||
composition.Register<IPasswordHasher, AspNetPasswordHasher>();
|
||||
composition.Register<IFilePermissionHelper, FilePermissionHelper>(Lifetime.Singleton);
|
||||
|
||||
composition.RegisterUnique<IHttpContextAccessor, AspNetHttpContextAccessor>(); // required for hybrid accessors
|
||||
composition.RegisterUnique<ICookieManager, AspNetCookieManager>();
|
||||
|
||||
|
||||
|
||||
composition.ComposeWebMappingProfiles();
|
||||
@@ -151,12 +126,17 @@ namespace Umbraco.Web.Runtime
|
||||
.AddTreeControllers(umbracoApiControllerTypes.Where(x => typeof(TreeControllerBase).IsAssignableFrom(x)));
|
||||
|
||||
|
||||
// Config manipulator
|
||||
composition.RegisterUnique<IConfigManipulator, XmlConfigManipulator>();
|
||||
|
||||
//ApplicationShutdownRegistry
|
||||
// STUFF that do not have to be moved to .NET CORE
|
||||
//----------------------------------------
|
||||
composition.RegisterUnique<ICookieManager, AspNetCookieManager>();
|
||||
composition.RegisterUnique<IApplicationShutdownRegistry, AspNetApplicationShutdownRegistry>();
|
||||
composition.RegisterUnique<IConfigManipulator, XmlConfigManipulator>();
|
||||
composition.RegisterUnique<IHttpContextAccessor, AspNetHttpContextAccessor>(); // required for hybrid accessors
|
||||
|
||||
composition.Register<AspNetSessionManager>(Lifetime.Singleton);
|
||||
composition.Register<ISessionIdResolver>(factory => factory.GetInstance<AspNetSessionManager>(), Lifetime.Singleton);
|
||||
composition.Register<ISessionManager>(factory => factory.GetInstance<AspNetSessionManager>(), Lifetime.Singleton);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user