mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-08-01 17:51:18 +08:00
Merge pull request #6232 from jtkech/patch-2
WorkContext & HttpContext: Unused registration and last minor refactoring for better clarity
This commit is contained in:
commit
0156aa8ff7
@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Runtime.Remoting;
|
using System.Runtime.Remoting;
|
||||||
using System.Runtime.Remoting.Messaging;
|
using System.Runtime.Remoting.Messaging;
|
||||||
@ -9,7 +9,7 @@ using Orchard.Mvc;
|
|||||||
using Orchard.Mvc.Extensions;
|
using Orchard.Mvc.Extensions;
|
||||||
|
|
||||||
namespace Orchard.Environment {
|
namespace Orchard.Environment {
|
||||||
public class WorkContextAccessor : IWorkContextAccessor {
|
public class WorkContextAccessor : ILogicalWorkContextAccessor {
|
||||||
readonly ILifetimeScope _lifetimeScope;
|
readonly ILifetimeScope _lifetimeScope;
|
||||||
|
|
||||||
readonly IHttpContextAccessor _httpContextAccessor;
|
readonly IHttpContextAccessor _httpContextAccessor;
|
||||||
@ -30,44 +30,34 @@ namespace Orchard.Environment {
|
|||||||
if (!httpContext.IsBackgroundContext())
|
if (!httpContext.IsBackgroundContext())
|
||||||
return httpContext.Items[_workContextKey] as WorkContext;
|
return httpContext.Items[_workContextKey] as WorkContext;
|
||||||
|
|
||||||
var context = CallContext.LogicalGetData(_workContextSlot) as ObjectHandle;
|
return GetLogicalContext();
|
||||||
return context != null ? context.Unwrap() as WorkContext : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public WorkContext GetContext() {
|
public WorkContext GetContext() {
|
||||||
var httpContext = _httpContextAccessor.Current();
|
var httpContext = _httpContextAccessor.Current();
|
||||||
if (!httpContext.IsBackgroundContext())
|
return GetContext(httpContext);
|
||||||
return GetContext(httpContext);
|
}
|
||||||
|
|
||||||
|
public WorkContext GetLogicalContext() {
|
||||||
var context = CallContext.LogicalGetData(_workContextSlot) as ObjectHandle;
|
var context = CallContext.LogicalGetData(_workContextSlot) as ObjectHandle;
|
||||||
return context != null ? context.Unwrap() as WorkContext : null;
|
return context != null ? context.Unwrap() as WorkContext : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IWorkContextScope CreateWorkContextScope(HttpContextBase httpContext) {
|
public IWorkContextScope CreateWorkContextScope(HttpContextBase httpContext) {
|
||||||
var workLifetime = _lifetimeScope.BeginLifetimeScope("work");
|
var workLifetime = _lifetimeScope.BeginLifetimeScope("work");
|
||||||
workLifetime.Resolve<WorkContextProperty<HttpContextBase>>().Value = httpContext;
|
|
||||||
|
|
||||||
var events = workLifetime.Resolve<IEnumerable<IWorkContextEvents>>();
|
var events = workLifetime.Resolve<IEnumerable<IWorkContextEvents>>();
|
||||||
events.Invoke(e => e.Started(), NullLogger.Instance);
|
events.Invoke(e => e.Started(), NullLogger.Instance);
|
||||||
|
|
||||||
return new HttpContextScopeImplementation(
|
if (!httpContext.IsBackgroundContext())
|
||||||
events,
|
return new HttpContextScopeImplementation(events, workLifetime, httpContext, _workContextKey);
|
||||||
workLifetime,
|
|
||||||
httpContext,
|
return new CallContextScopeImplementation(events, workLifetime, _workContextSlot);
|
||||||
_workContextKey);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IWorkContextScope CreateWorkContextScope() {
|
public IWorkContextScope CreateWorkContextScope() {
|
||||||
var httpContext = _httpContextAccessor.Current();
|
var httpContext = _httpContextAccessor.Current();
|
||||||
if (!httpContext.IsBackgroundContext())
|
return CreateWorkContextScope(httpContext);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class HttpContextScopeImplementation : IWorkContextScope {
|
class HttpContextScopeImplementation : IWorkContextScope {
|
||||||
@ -80,7 +70,6 @@ namespace Orchard.Environment {
|
|||||||
|
|
||||||
_disposer = () => {
|
_disposer = () => {
|
||||||
events.Invoke(e => e.Finished(), NullLogger.Instance);
|
events.Invoke(e => e.Finished(), NullLogger.Instance);
|
||||||
|
|
||||||
httpContext.Items.Remove(workContextKey);
|
httpContext.Items.Remove(workContextKey);
|
||||||
lifetimeScope.Dispose();
|
lifetimeScope.Dispose();
|
||||||
};
|
};
|
||||||
|
@ -19,10 +19,6 @@ namespace Orchard.Environment {
|
|||||||
.As<WorkContext>()
|
.As<WorkContext>()
|
||||||
.InstancePerMatchingLifetimeScope("work");
|
.InstancePerMatchingLifetimeScope("work");
|
||||||
|
|
||||||
builder.RegisterType<WorkContextProperty<HttpContextBase>>()
|
|
||||||
.As<WorkContextProperty<HttpContextBase>>()
|
|
||||||
.InstancePerMatchingLifetimeScope("work");
|
|
||||||
|
|
||||||
builder.RegisterGeneric(typeof(WorkValues<>))
|
builder.RegisterGeneric(typeof(WorkValues<>))
|
||||||
.InstancePerMatchingLifetimeScope("work");
|
.InstancePerMatchingLifetimeScope("work");
|
||||||
|
|
||||||
|
@ -10,6 +10,10 @@ namespace Orchard {
|
|||||||
IWorkContextScope CreateWorkContextScope();
|
IWorkContextScope CreateWorkContextScope();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface ILogicalWorkContextAccessor: IWorkContextAccessor {
|
||||||
|
WorkContext GetLogicalContext();
|
||||||
|
}
|
||||||
|
|
||||||
public interface IWorkContextStateProvider : IDependency {
|
public interface IWorkContextStateProvider : IDependency {
|
||||||
Func<WorkContext, T> Get<T>(string name);
|
Func<WorkContext, T> Get<T>(string name);
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ namespace Orchard.Mvc {
|
|||||||
if (_wca == null && _lifetimeScope.IsRegistered<IWorkContextAccessor>())
|
if (_wca == null && _lifetimeScope.IsRegistered<IWorkContextAccessor>())
|
||||||
_wca = _lifetimeScope.Resolve<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;
|
return workContext != null ? workContext.HttpContext : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,8 +19,7 @@ namespace Orchard.Tasks {
|
|||||||
public BackgroundService(
|
public BackgroundService(
|
||||||
IEnumerable<IBackgroundTask> tasks,
|
IEnumerable<IBackgroundTask> tasks,
|
||||||
ITransactionManager transactionManager,
|
ITransactionManager transactionManager,
|
||||||
ShellSettings shellSettings,
|
ShellSettings shellSettings) {
|
||||||
IBackgroundHttpContextFactory backgroundHttpContextFactory) {
|
|
||||||
|
|
||||||
_tasks = tasks;
|
_tasks = tasks;
|
||||||
_transactionManager = transactionManager;
|
_transactionManager = transactionManager;
|
||||||
|
@ -10,6 +10,11 @@ namespace Orchard {
|
|||||||
return workContextAccessor.GetContext(controllerContext.RequestContext.HttpContext);
|
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) {
|
public static WorkContext GetWorkContext(this RequestContext requestContext) {
|
||||||
if (requestContext == null)
|
if (requestContext == null)
|
||||||
return null;
|
return null;
|
||||||
|
Loading…
Reference in New Issue
Block a user