Merge pull request #1129 from umbraco/temp-U4-7577
U4-7577 Remove dependency on UmbracoContext in CreateServiceContext /…
This commit is contained in:
15
src/Umbraco.Web/IHttpContextAccessor.cs
Normal file
15
src/Umbraco.Web/IHttpContextAccessor.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System.Web;
|
||||
|
||||
namespace Umbraco.Web
|
||||
{
|
||||
/// <summary>
|
||||
/// Used to retrieve the HttpContext
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// NOTE: This has a singleton lifespan
|
||||
/// </remarks>
|
||||
public interface IHttpContextAccessor
|
||||
{
|
||||
HttpContextBase Value { get; }
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,10 @@ namespace Umbraco.Web
|
||||
{
|
||||
/// <summary>
|
||||
/// Used to retrieve the Umbraco context
|
||||
/// </summary>
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// NOTE: This has a singleton lifespan
|
||||
/// </remarks>
|
||||
public interface IUmbracoContextAccessor
|
||||
{
|
||||
UmbracoContext Value { get; }
|
||||
|
||||
@@ -8,21 +8,21 @@ namespace Umbraco.Web
|
||||
/// </summary>
|
||||
internal class RequestLifespanMessagesFactory : IEventMessagesFactory
|
||||
{
|
||||
private readonly IUmbracoContextAccessor _ctxAccessor;
|
||||
private readonly IHttpContextAccessor _httpAccessor;
|
||||
|
||||
public RequestLifespanMessagesFactory(IUmbracoContextAccessor ctxAccessor)
|
||||
public RequestLifespanMessagesFactory(IHttpContextAccessor httpAccessor)
|
||||
{
|
||||
if (ctxAccessor == null) throw new ArgumentNullException("ctxAccessor");
|
||||
_ctxAccessor = ctxAccessor;
|
||||
if (httpAccessor == null) throw new ArgumentNullException("httpAccessor");
|
||||
_httpAccessor = httpAccessor;
|
||||
}
|
||||
|
||||
public EventMessages Get()
|
||||
{
|
||||
if (_ctxAccessor.Value.HttpContext.Items[typeof (RequestLifespanMessagesFactory).Name] == null)
|
||||
if (_httpAccessor.Value.Items[typeof (RequestLifespanMessagesFactory).Name] == null)
|
||||
{
|
||||
_ctxAccessor.Value.HttpContext.Items[typeof(RequestLifespanMessagesFactory).Name] = new EventMessages();
|
||||
_httpAccessor.Value.Items[typeof(RequestLifespanMessagesFactory).Name] = new EventMessages();
|
||||
}
|
||||
return (EventMessages)_ctxAccessor.Value.HttpContext.Items[typeof (RequestLifespanMessagesFactory).Name];
|
||||
return (EventMessages)_httpAccessor.Value.Items[typeof (RequestLifespanMessagesFactory).Name];
|
||||
}
|
||||
}
|
||||
}
|
||||
12
src/Umbraco.Web/SingletonHttpContextAccessor.cs
Normal file
12
src/Umbraco.Web/SingletonHttpContextAccessor.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Web;
|
||||
|
||||
namespace Umbraco.Web
|
||||
{
|
||||
internal class SingletonHttpContextAccessor : IHttpContextAccessor
|
||||
{
|
||||
public HttpContextBase Value
|
||||
{
|
||||
get { return new HttpContextWrapper(HttpContext.Current); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -318,6 +318,7 @@
|
||||
<Compile Include="Editors\BackOfficeNotificationsController.cs" />
|
||||
<Compile Include="Editors\EditorValidationResolver.cs" />
|
||||
<Compile Include="Editors\EditorValidator.cs" />
|
||||
<Compile Include="IHttpContextAccessor.cs" />
|
||||
<Compile Include="Models\ContentEditing\GetAvailableCompositionsFilter.cs" />
|
||||
<Compile Include="Editors\IEditorValidator.cs" />
|
||||
<Compile Include="Editors\EditorModelEventManager.cs" />
|
||||
@@ -365,6 +366,7 @@
|
||||
<Compile Include="Media\EmbedProviders\OEmbedPhoto.cs" />
|
||||
<Compile Include="Security\Identity\IUmbracoBackOfficeTwoFactorOptions.cs" />
|
||||
<Compile Include="Models\Mapping\PropertyTypeGroupResolver.cs" />
|
||||
<Compile Include="SingletonHttpContextAccessor.cs" />
|
||||
<Compile Include="Trees\ContentTypeTreeController.cs" />
|
||||
<Compile Include="Trees\MediaTypeTreeController.cs" />
|
||||
<Compile Include="Trees\MemberTypeTreeController.cs" />
|
||||
|
||||
@@ -89,7 +89,7 @@ namespace Umbraco.Web
|
||||
protected override ServiceContext CreateServiceContext(DatabaseContext dbContext, IDatabaseFactory dbFactory)
|
||||
{
|
||||
//use a request based messaging factory
|
||||
var evtMsgs = new RequestLifespanMessagesFactory(new SingletonUmbracoContextAccessor());
|
||||
var evtMsgs = new RequestLifespanMessagesFactory(new SingletonHttpContextAccessor());
|
||||
return new ServiceContext(
|
||||
new RepositoryFactory(ApplicationCache, ProfilingLogger.Logger, dbContext.SqlSyntax, UmbracoConfig.For.UmbracoSettings()),
|
||||
new PetaPocoUnitOfWorkProvider(dbFactory),
|
||||
|
||||
Reference in New Issue
Block a user