From 03f68cefb7ecc2cc1534ebb063d0a1a35e0525c2 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 22 Sep 2015 14:39:55 +0200 Subject: [PATCH] performs a fallback check for HttpContext in the batched server messengers in some cases where cache refreshers are executed on async threads... though there's not much we can do about when they are fired on background threads. --- src/Umbraco.Web/BatchedDatabaseServerMessenger.cs | 12 ++++++++++-- src/Umbraco.Web/BatchedWebServiceServerMessenger.cs | 13 +++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Umbraco.Web/BatchedDatabaseServerMessenger.cs b/src/Umbraco.Web/BatchedDatabaseServerMessenger.cs index 714b79514d..00dc4e8063 100644 --- a/src/Umbraco.Web/BatchedDatabaseServerMessenger.cs +++ b/src/Umbraco.Web/BatchedDatabaseServerMessenger.cs @@ -98,11 +98,19 @@ namespace Umbraco.Web protected ICollection GetBatch(bool ensureHttpContext) { - var httpContext = UmbracoContext.Current == null ? null : UmbracoContext.Current.HttpContext; + //try get the http context from the UmbracoContext, we do this because in the case we are launching an async + // thread and we know that the cache refreshers will execute, we will ensure the UmbracoContext and therefore we + // can get the http context from it + var httpContext = (UmbracoContext.Current == null ? null : UmbracoContext.Current.HttpContext) + //if this is null, it could be that an async thread is calling this method that we weren't aware of and the UmbracoContext + // wasn't ensured at the beginning of the thread. We can try to see if the HttpContext.Current is available which might be + // the case if the asp.net synchronization context has kicked in + ?? (HttpContext.Current == null ? null : new HttpContextWrapper(HttpContext.Current)); + if (httpContext == null) { if (ensureHttpContext) - throw new NotSupportedException("Cannot execute without a valid/current UmbracoContext with an HttpContext assigned."); + throw new NotSupportedException("Cannot execute without a valid/current HttpContext assigned."); return null; } diff --git a/src/Umbraco.Web/BatchedWebServiceServerMessenger.cs b/src/Umbraco.Web/BatchedWebServiceServerMessenger.cs index 5fc3b48eee..63536ca9f7 100644 --- a/src/Umbraco.Web/BatchedWebServiceServerMessenger.cs +++ b/src/Umbraco.Web/BatchedWebServiceServerMessenger.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Web; using Umbraco.Core.Sync; namespace Umbraco.Web @@ -38,11 +39,19 @@ namespace Umbraco.Web protected override ICollection GetBatch(bool ensureHttpContext) { - var httpContext = UmbracoContext.Current == null ? null : UmbracoContext.Current.HttpContext; + //try get the http context from the UmbracoContext, we do this because in the case we are launching an async + // thread and we know that the cache refreshers will execute, we will ensure the UmbracoContext and therefore we + // can get the http context from it + var httpContext = (UmbracoContext.Current == null ? null : UmbracoContext.Current.HttpContext) + //if this is null, it could be that an async thread is calling this method that we weren't aware of and the UmbracoContext + // wasn't ensured at the beginning of the thread. We can try to see if the HttpContext.Current is available which might be + // the case if the asp.net synchronization context has kicked in + ?? (HttpContext.Current == null ? null : new HttpContextWrapper(HttpContext.Current)); + if (httpContext == null) { if (ensureHttpContext) - throw new NotSupportedException("Cannot execute without a valid/current UmbracoContext with an HttpContext assigned."); + throw new NotSupportedException("Cannot execute without a valid/current HttpContext assigned."); return null; }