Merge pull request #6232 from jtkech/patch-2

WorkContext & HttpContext: Unused registration and last minor refactoring for better clarity
This commit is contained in:
Sébastien Ros 2016-01-07 12:55:20 -08:00
commit 0156aa8ff7
6 changed files with 22 additions and 29 deletions

View File

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
@ -9,7 +9,7 @@ using Orchard.Mvc;
using Orchard.Mvc.Extensions;
namespace Orchard.Environment {
public class WorkContextAccessor : IWorkContextAccessor {
public class WorkContextAccessor : ILogicalWorkContextAccessor {
readonly ILifetimeScope _lifetimeScope;
readonly IHttpContextAccessor _httpContextAccessor;
@ -30,44 +30,34 @@ namespace Orchard.Environment {
if (!httpContext.IsBackgroundContext())
return httpContext.Items[_workContextKey] as WorkContext;
var context = CallContext.LogicalGetData(_workContextSlot) as ObjectHandle;
return context != null ? context.Unwrap() as WorkContext : null;
return GetLogicalContext();
}
public WorkContext GetContext() {
var httpContext = _httpContextAccessor.Current();
if (!httpContext.IsBackgroundContext())
return GetContext(httpContext);
return GetContext(httpContext);
}
public WorkContext GetLogicalContext() {
var context = CallContext.LogicalGetData(_workContextSlot) as ObjectHandle;
return context != null ? context.Unwrap() as WorkContext : null;
}
public IWorkContextScope CreateWorkContextScope(HttpContextBase httpContext) {
var workLifetime = _lifetimeScope.BeginLifetimeScope("work");
workLifetime.Resolve<WorkContextProperty<HttpContextBase>>().Value = httpContext;
var events = workLifetime.Resolve<IEnumerable<IWorkContextEvents>>();
events.Invoke(e => e.Started(), NullLogger.Instance);
return new HttpContextScopeImplementation(
events,
workLifetime,
httpContext,
_workContextKey);
if (!httpContext.IsBackgroundContext())
return new HttpContextScopeImplementation(events, workLifetime, httpContext, _workContextKey);
return new CallContextScopeImplementation(events, workLifetime, _workContextSlot);
}
public IWorkContextScope CreateWorkContextScope() {
var httpContext = _httpContextAccessor.Current();
if (!httpContext.IsBackgroundContext())
return CreateWorkContextScope(httpContext);
var workLifetime = _lifetimeScope.BeginLifetimeScope("work");
var events = workLifetime.Resolve<IEnumerable<IWorkContextEvents>>();
events.Invoke(e => e.Started(), NullLogger.Instance);
return new CallContextScopeImplementation(events, workLifetime, _workContextSlot);
return CreateWorkContextScope(httpContext);
}
class HttpContextScopeImplementation : IWorkContextScope {
@ -80,7 +70,6 @@ namespace Orchard.Environment {
_disposer = () => {
events.Invoke(e => e.Finished(), NullLogger.Instance);
httpContext.Items.Remove(workContextKey);
lifetimeScope.Dispose();
};

View File

@ -19,10 +19,6 @@ namespace Orchard.Environment {
.As<WorkContext>()
.InstancePerMatchingLifetimeScope("work");
builder.RegisterType<WorkContextProperty<HttpContextBase>>()
.As<WorkContextProperty<HttpContextBase>>()
.InstancePerMatchingLifetimeScope("work");
builder.RegisterGeneric(typeof(WorkValues<>))
.InstancePerMatchingLifetimeScope("work");

View File

@ -10,6 +10,10 @@ namespace Orchard {
IWorkContextScope CreateWorkContextScope();
}
public interface ILogicalWorkContextAccessor: IWorkContextAccessor {
WorkContext GetLogicalContext();
}
public interface IWorkContextStateProvider : IDependency {
Func<WorkContext, T> Get<T>(string name);
}

View File

@ -24,7 +24,7 @@ namespace Orchard.Mvc {
if (_wca == null && _lifetimeScope.IsRegistered<IWorkContextAccessor>())
_wca = _lifetimeScope.Resolve<IWorkContextAccessor>();
var workContext = _wca != null ? _wca.GetContext(null) : null;
var workContext = _wca != null ? _wca.GetLogicalContext() : null;
return workContext != null ? workContext.HttpContext : null;
}

View File

@ -19,8 +19,7 @@ namespace Orchard.Tasks {
public BackgroundService(
IEnumerable<IBackgroundTask> tasks,
ITransactionManager transactionManager,
ShellSettings shellSettings,
IBackgroundHttpContextFactory backgroundHttpContextFactory) {
ShellSettings shellSettings) {
_tasks = tasks;
_transactionManager = transactionManager;

View File

@ -10,6 +10,11 @@ namespace Orchard {
return workContextAccessor.GetContext(controllerContext.RequestContext.HttpContext);
}
public static WorkContext GetLogicalContext(this IWorkContextAccessor workContextAccessor) {
var wca = workContextAccessor as ILogicalWorkContextAccessor;
return wca != null ? wca.GetLogicalContext() : null;
}
public static WorkContext GetWorkContext(this RequestContext requestContext) {
if (requestContext == null)
return null;