mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-04-05 21:01:35 +08:00
parent
b9fa87d5e2
commit
59d20c3d40
@ -1,33 +0,0 @@
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using Orchard.Settings;
|
||||
|
||||
namespace Orchard {
|
||||
/// <summary>
|
||||
/// A factory class that creates an HttpContext instance and initializes the HttpContext.Current property with that instance.
|
||||
/// This is useful when rendering views from a background thread, as some Html Helpers access HttpContext.Current directly, thus preventing a NullReferenceException.
|
||||
/// </summary>
|
||||
public class BackgroundHttpContextFactory : IBackgroundHttpContextFactory {
|
||||
public const string IsBackgroundHttpContextKey = "IsBackgroundHttpContext";
|
||||
private readonly ISiteService _siteService;
|
||||
public BackgroundHttpContextFactory(ISiteService siteService) {
|
||||
_siteService = siteService;
|
||||
}
|
||||
|
||||
public HttpContext CreateHttpContext() {
|
||||
var url = _siteService.GetSiteSettings().BaseUrl;
|
||||
var httpContext = new HttpContext(new HttpRequest("", url, ""), new HttpResponse(new StringWriter()));
|
||||
|
||||
httpContext.Items[IsBackgroundHttpContextKey] = true;
|
||||
|
||||
return httpContext;
|
||||
}
|
||||
|
||||
public void InitializeHttpContext() {
|
||||
if (HttpContext.Current != null)
|
||||
return;
|
||||
|
||||
HttpContext.Current = CreateHttpContext();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Runtime.Remoting.Messaging;
|
||||
using System.Web;
|
||||
using Orchard.Mvc;
|
||||
|
||||
namespace Orchard.Environment.State {
|
||||
|
||||
@ -22,7 +23,7 @@ namespace Orchard.Environment.State {
|
||||
}
|
||||
|
||||
public T GetState() {
|
||||
if (HttpContext.Current == null) {
|
||||
if (!HttpContextIsValid()) {
|
||||
var data = CallContext.GetData(_name);
|
||||
|
||||
if (data == null) {
|
||||
@ -44,13 +45,16 @@ namespace Orchard.Environment.State {
|
||||
}
|
||||
|
||||
public void SetState(T state) {
|
||||
if (HttpContext.Current == null) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
using System.Web;
|
||||
|
||||
namespace Orchard {
|
||||
public interface IBackgroundHttpContextFactory : IDependency {
|
||||
HttpContext CreateHttpContext();
|
||||
void InitializeHttpContext();
|
||||
}
|
||||
}
|
@ -168,7 +168,6 @@
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="BackgroundHttpContextFactory.cs" />
|
||||
<Compile Include="ContentManagement\Handlers\CloneContentContext.cs" />
|
||||
<Compile Include="Environment\Configuration\ExtensionLocations.cs" />
|
||||
<Compile Include="DisplayManagement\IPositioned.cs" />
|
||||
@ -177,7 +176,6 @@
|
||||
<Compile Include="Data\Migration\Schema\DropUniqueConstraintCommand.cs" />
|
||||
<Compile Include="Environment\Extensions\Models\LifecycleStatus.cs" />
|
||||
<Compile Include="Environment\ShellBuilders\ICompositionStrategy.cs" />
|
||||
<Compile Include="IBackgroundHttpContextFactory.cs" />
|
||||
<Compile Include="Mvc\Updater.cs" />
|
||||
<Compile Include="Recipes\Models\ConfigurationContext.cs" />
|
||||
<Compile Include="Recipes\Models\RecipeBuilderStepConfigurationContext.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user