#4607: Refactored HttpContextAccessor to return a valid HttpContextBase instance in background tasks.

This commit is contained in:
Sipke Schoorstra
2015-05-26 19:49:55 +02:00
parent d1ae7380e4
commit 9bf6ac5718
11 changed files with 46 additions and 44 deletions

View File

@@ -2,6 +2,7 @@
using Orchard.ContentManagement;
using Orchard.Core.Settings.Models;
using Orchard.Mvc;
using Orchard.Mvc.Extensions;
using Orchard.Settings;
using Orchard.Utility.Extensions;
@@ -38,7 +39,7 @@ namespace Orchard.Core.Settings.Commands {
// Retrieve request URL if BaseUrl not provided as a switch value
if (string.IsNullOrEmpty(BaseUrl)) {
if (_httpContextAccessor.Current() == null) {
if (_httpContextAccessor.Current().IsBackgroundContext()) {
Context.Output.WriteLine(T("No HTTP request available to determine the base url of the site"));
return;
}

View File

@@ -11,6 +11,7 @@ using Orchard.DisplayManagement.Shapes;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Mvc;
using Orchard.Mvc.Extensions;
namespace Orchard.DisplayManagement.Implementation {
public class DefaultDisplayManager : IDisplayManager {
@@ -61,7 +62,7 @@ namespace Orchard.DisplayManagement.Implementation {
return CoerceHtmlString(context.Value);
var workContext = _workContextAccessor.GetContext();
var shapeTable = _httpContextAccessor.Current() != null
var shapeTable = !_httpContextAccessor.Current().IsBackgroundContext()
? _shapeTableLocator.Value.Lookup(workContext.CurrentTheme.Id)
: _shapeTableLocator.Value.Lookup(null);

View File

@@ -3,6 +3,7 @@ using System.Web;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Mvc;
using Orchard.Mvc.Extensions;
using Orchard.Services;
using Orchard.Utility.Extensions;
@@ -41,7 +42,7 @@ namespace Orchard.Environment {
// current request can be processed correctly. So, we redirect to the same URL, so that the
// new request will come to the newly started AppDomain.
var httpContext = _httpContextAccessor.Current();
if (httpContext != null) {
if (!httpContext.IsBackgroundContext()) {
// Don't redirect posts...
if (httpContext.Request.RequestType == "GET") {
httpContext.Response.Redirect(HttpContext.Current.Request.ToUrlString(), true /*endResponse*/);

View File

@@ -63,7 +63,7 @@ namespace Orchard.Environment {
builder.RegisterType<AppDomainAssemblyNameResolver>().As<IAssemblyNameResolver>().SingleInstance();
builder.RegisterType<GacAssemblyNameResolver>().As<IAssemblyNameResolver>().SingleInstance();
builder.RegisterType<OrchardFrameworkAssemblyNameResolver>().As<IAssemblyNameResolver>().SingleInstance();
builder.RegisterType<HttpContextAccessor>().As<IHttpContextAccessor>().SingleInstance();
builder.RegisterType<HttpContextAccessor>().As<IHttpContextAccessor>().InstancePerDependency();
builder.RegisterType<ViewsBackgroundCompilation>().As<IViewsBackgroundCompilation>().SingleInstance();
builder.RegisterType<DefaultExceptionPolicy>().As<IExceptionPolicy>().SingleInstance();
builder.RegisterType<DefaultCriticalErrorProvider>().As<ICriticalErrorProvider>().SingleInstance();

View File

@@ -5,6 +5,7 @@ using System.Web;
using Autofac;
using Orchard.Logging;
using Orchard.Mvc;
using Orchard.Mvc.Extensions;
namespace Orchard.Environment {
public class WorkContextAccessor : IWorkContextAccessor {
@@ -31,7 +32,7 @@ namespace Orchard.Environment {
public WorkContext GetContext() {
var httpContext = _httpContextAccessor.Current();
if (httpContext != null)
if (!httpContext.IsBackgroundContext())
return GetContext(httpContext);
WorkContext workContext;
@@ -55,7 +56,7 @@ namespace Orchard.Environment {
public IWorkContextScope CreateWorkContextScope() {
var httpContext = _httpContextAccessor.Current();
if (httpContext != null)
if (!httpContext.IsBackgroundContext())
return CreateWorkContextScope(httpContext);
var workLifetime = _lifetimeScope.BeginLifetimeScope("work");

View File

@@ -0,0 +1,9 @@
using System.Web;
namespace Orchard.Mvc.Extensions {
public static class HttpContextBaseExtensions {
public static bool IsBackgroundContext(this HttpContextBase httpContextBase) {
return httpContextBase == null || httpContextBase is MvcModule.HttpContextPlaceholder;
}
}
}

View File

@@ -0,0 +1,18 @@
using System.Web;
using Autofac;
namespace Orchard.Mvc {
public class HttpContextAccessor : IHttpContextAccessor {
private readonly IComponentContext _context;
public HttpContextAccessor(IComponentContext context) {
_context = context;
}
public HttpContextBase Current() {
HttpContextBase httpContextBase;
_context.TryResolve(out httpContextBase);
return httpContextBase;
}
}
}

View File

@@ -1,39 +1,7 @@
using System;
using System.Web;
using System.Web;
namespace Orchard.Mvc {
public interface IHttpContextAccessor {
HttpContextBase Current();
void Set(HttpContextBase httpContext);
}
public class HttpContextAccessor : IHttpContextAccessor {
private HttpContextBase _httpContext;
public HttpContextBase Current() {
var httpContext = GetStaticProperty();
return httpContext != null ? new HttpContextWrapper(httpContext) : _httpContext;
}
public void Set(HttpContextBase httpContext) {
_httpContext = httpContext;
}
private HttpContext GetStaticProperty() {
var httpContext = HttpContext.Current;
if (httpContext == null) {
return null;
}
try {
if (httpContext.Request == null) {
return null;
}
}
catch (Exception) {
return null;
}
return httpContext;
}
}
}

View File

@@ -85,7 +85,7 @@ namespace Orchard.Mvc {
/// <summary>
/// Standin context for background tasks.
/// </summary>
class HttpContextPlaceholder : HttpContextBase {
public class HttpContextPlaceholder : HttpContextBase {
private readonly Lazy<string> _baseUrl;
private readonly IDictionary _items = new Dictionary<object, object>();
@@ -120,7 +120,7 @@ namespace Orchard.Mvc {
}
}
private class HttpResponsePlaceholder : HttpResponseBase {
public class HttpResponsePlaceholder : HttpResponseBase {
public override string ApplyAppPathModifier(string virtualPath) {
return virtualPath;
}
@@ -135,7 +135,7 @@ namespace Orchard.Mvc {
/// <summary>
/// standin context for background tasks.
/// </summary>
class HttpRequestPlaceholder : HttpRequestBase {
public class HttpRequestPlaceholder : HttpRequestBase {
private readonly Uri _uri;
public HttpRequestPlaceholder(Uri uri) {
@@ -217,7 +217,7 @@ namespace Orchard.Mvc {
}
}
class HttpBrowserCapabilitiesPlaceholder : HttpBrowserCapabilitiesBase {
public class HttpBrowserCapabilitiesPlaceholder : HttpBrowserCapabilitiesBase {
public override string this[string key] {
get {
return "";

View File

@@ -317,7 +317,9 @@
<Compile Include="Mvc\DataAnnotations\LocalizedModelValidatorProvider.cs" />
<Compile Include="Mvc\DataAnnotations\LocalizedRequiredAttribute.cs" />
<Compile Include="Mvc\Extensions\RouteExtension.cs" />
<Compile Include="Mvc\Extensions\HttpContextBaseExtensions.cs" />
<Compile Include="Mvc\FormValueRequiredAttribute.cs" />
<Compile Include="Mvc\HttpContextAccessor.cs" />
<Compile Include="Mvc\HttpContextWorkContext.cs" />
<Compile Include="Mvc\Extensions\ControllerExtensions.cs" />
<Compile Include="Mvc\IOrchardViewPage.cs" />

View File

@@ -5,6 +5,7 @@ using Orchard.Environment.Configuration;
using Orchard.Logging;
using Orchard.ContentManagement;
using Orchard.Mvc;
using Orchard.Mvc.Extensions;
using Orchard.Services;
namespace Orchard.Security.Providers {
@@ -102,7 +103,7 @@ namespace Orchard.Security.Providers {
return _signedInUser;
var httpContext = _httpContextAccessor.Current();
if (httpContext == null || !httpContext.Request.IsAuthenticated || !(httpContext.User.Identity is FormsIdentity)) {
if (httpContext.IsBackgroundContext() || !httpContext.Request.IsAuthenticated || !(httpContext.User.Identity is FormsIdentity)) {
return null;
}