Fixing a stack overflow exception when the HttpContext is mocked (for Orchard command line and Background processed) when accessing the site settings (which is needed to for the HttpContext stand-in).

This commit is contained in:
Sipke Schoorstra
2013-11-20 01:31:50 +01:00
parent b751ef7216
commit cd6bf2c357

View File

@@ -5,9 +5,6 @@ using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Orchard.ContentManagement;
using Orchard.Data;
using Orchard.Environment.Configuration;
using Orchard.Mvc.Routes;
using Orchard.Settings;
@@ -42,19 +39,13 @@ namespace Orchard.Mvc {
return new HttpContextWrapper(HttpContext.Current);
}
//this doesn't work in a background service, throws an exception in ContentManager.Handlers
var siteService = context.Resolve<ISiteService>();
var baseUrl = siteService.GetSiteSettings().BaseUrl;
//var session = context.Resolve<ISessionLocator>().For(typeof(ContentItem));
//var shellSettings = context.Resolve<ShellSettings>();
//var tableName = "Settings_SiteSettings2PartRecord";
//if (!string.IsNullOrEmpty(shellSettings.DataTablePrefix)) {
// tableName = shellSettings.DataTablePrefix + "_" + tableName;
//}
//var query = session.CreateSQLQuery("SELECT BaseUrl FROM " + tableName);
//var baseUrl = query.UniqueResult<string>();
// Wrapping the code accessing the SiteSettings in a function that will be executed later (in HttpContextPlaceholder),
// so that the RequestContext will have been established when the time comes to actually load the site settings,
// which requires activating the Site content item, which in turn requires a UrlHelper, which in turn requires a RequestContext,
// thus preventing a StackOverflowException.
var baseUrl = new Func<string>(() => siteService.GetSiteSettings().BaseUrl);
return new HttpContextPlaceholder(baseUrl);
}
@@ -90,14 +81,14 @@ namespace Orchard.Mvc {
/// standin context for background tasks.
/// </summary>
class HttpContextPlaceholder : HttpContextBase {
private readonly string _baseUrl;
private readonly Lazy<string> _baseUrl;
public HttpContextPlaceholder(string baseUrl) {
_baseUrl = baseUrl;
public HttpContextPlaceholder(Func<string> baseUrl) {
_baseUrl = new Lazy<string>(baseUrl);
}
public override HttpRequestBase Request {
get { return new HttpRequestPlaceholder(new Uri(_baseUrl)); }
get { return new HttpRequestPlaceholder(new Uri(_baseUrl.Value)); }
}
public override IHttpHandler Handler { get; set; }