From b0ce589f941337636d2f5d00db9fe9d02c998e24 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Fri, 19 Feb 2016 12:54:45 +0100 Subject: [PATCH] Reverted 42e71fe9fa2cc5337eb9239d68a08a2b680f3df2 Unfortunately this is breaking the setup. For some reason, when executed in a regular HTTP context, CallContext "loses" its data at the end of the request, causing recipe execution to fail. Until we understand why this is and how to best deal with it, I am reverting this file so that it at least works. --- src/Orchard/Environment/State/ContextState.cs | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/Orchard/Environment/State/ContextState.cs b/src/Orchard/Environment/State/ContextState.cs index 8fee2c7e3..70310d3f9 100644 --- a/src/Orchard/Environment/State/ContextState.cs +++ b/src/Orchard/Environment/State/ContextState.cs @@ -1,11 +1,12 @@ using System; -using System.Runtime.Remoting; using System.Runtime.Remoting.Messaging; +using System.Web; +using Orchard.Mvc; namespace Orchard.Environment.State { /// - /// Holds some state through the logical call context + /// Holds some state for the current HttpContext or thread /// /// The type of data to store public class ContextState where T : class { @@ -22,21 +23,38 @@ namespace Orchard.Environment.State { } public T GetState() { - var handle = CallContext.LogicalGetData(_name) as ObjectHandle; - var data = handle != null ? handle.Unwrap() : null; + if (!HttpContextIsValid()) { + var data = CallContext.GetData(_name); - if (data == null) { - if (_defaultValue != null) { - CallContext.LogicalSetData(_name, new ObjectHandle(data = _defaultValue())); - return data as T; + if (data == null) { + if (_defaultValue != null) { + CallContext.SetData(_name, data = _defaultValue()); + return data as T; + } } + + return data as T; } - return data as T; + if (HttpContext.Current.Items[_name] == null) { + HttpContext.Current.Items[_name] = _defaultValue == null ? null : _defaultValue(); + } + + return HttpContext.Current.Items[_name] as T; + } public void SetState(T state) { - CallContext.LogicalSetData(_name, new ObjectHandle(state)); + if (!HttpContextIsValid()) { + CallContext.SetData(_name, state); + } + else { + HttpContext.Current.Items[_name] = state; + } + } + + private bool HttpContextIsValid() { + return HttpContext.Current != null && !HttpContext.Current.Items.Contains(MvcModule.IsBackgroundHttpContextKey); } } -} +} \ No newline at end of file