diff --git a/src/Orchard/Mvc/MvcModule.cs b/src/Orchard/Mvc/MvcModule.cs index 9630473da..e33195b56 100644 --- a/src/Orchard/Mvc/MvcModule.cs +++ b/src/Orchard/Mvc/MvcModule.cs @@ -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(); - var baseUrl = siteService.GetSiteSettings().BaseUrl; - //var session = context.Resolve().For(typeof(ContentItem)); - //var shellSettings = context.Resolve(); - - //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(); + // 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(() => siteService.GetSiteSettings().BaseUrl); return new HttpContextPlaceholder(baseUrl); } @@ -90,14 +81,14 @@ namespace Orchard.Mvc { /// standin context for background tasks. /// class HttpContextPlaceholder : HttpContextBase { - private readonly string _baseUrl; + private readonly Lazy _baseUrl; - public HttpContextPlaceholder(string baseUrl) { - _baseUrl = baseUrl; + public HttpContextPlaceholder(Func baseUrl) { + _baseUrl = new Lazy(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; }