mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-07-17 00:38:22 +08:00
commit
d010e97b9d
@ -1,12 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Runtime.Remoting;
|
||||||
using System.Runtime.Remoting.Messaging;
|
using System.Runtime.Remoting.Messaging;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
using Orchard.Mvc;
|
using Orchard.Mvc.Extensions;
|
||||||
|
|
||||||
namespace Orchard.Environment.State {
|
namespace Orchard.Environment.State {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Holds some state for the current HttpContext or thread
|
/// Holds some state for the current HttpContext or Logical Context
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The type of data to store</typeparam>
|
/// <typeparam name="T">The type of data to store</typeparam>
|
||||||
public class ContextState<T> where T : class {
|
public class ContextState<T> where T : class {
|
||||||
@ -23,12 +23,13 @@ namespace Orchard.Environment.State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public T GetState() {
|
public T GetState() {
|
||||||
if (!HttpContextIsValid()) {
|
if (HttpContext.Current.IsBackgroundHttpContext()) {
|
||||||
var data = CallContext.GetData(_name);
|
var handle = CallContext.LogicalGetData(_name) as ObjectHandle;
|
||||||
|
var data = handle != null ? handle.Unwrap() : null;
|
||||||
|
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
if (_defaultValue != null) {
|
if (_defaultValue != null) {
|
||||||
CallContext.SetData(_name, data = _defaultValue());
|
CallContext.LogicalSetData(_name, new ObjectHandle(data = _defaultValue()));
|
||||||
return data as T;
|
return data as T;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -41,20 +42,15 @@ namespace Orchard.Environment.State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return HttpContext.Current.Items[_name] as T;
|
return HttpContext.Current.Items[_name] as T;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetState(T state) {
|
public void SetState(T state) {
|
||||||
if (!HttpContextIsValid()) {
|
if (HttpContext.Current.IsBackgroundHttpContext()) {
|
||||||
CallContext.SetData(_name, state);
|
CallContext.LogicalSetData(_name, new ObjectHandle(state));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
HttpContext.Current.Items[_name] = state;
|
HttpContext.Current.Items[_name] = state;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool HttpContextIsValid() {
|
|
||||||
return HttpContext.Current != null && !HttpContext.Current.Items.Contains(MvcModule.IsBackgroundHttpContextKey);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user