ensure appctx and security isn't nulled on disposal (it's an application singleton it doesn't need to be nulled), creates new CreateContext method for creating standalone UmbracoContext instances.

This commit is contained in:
Shannon
2016-01-23 12:38:08 +01:00
parent 20dc4f5bc4
commit b5e8cb29dd

View File

@@ -125,6 +125,7 @@ namespace Umbraco.Web
if (umbracoSettings == null) throw new ArgumentNullException("umbracoSettings");
if (urlProviders == null) throw new ArgumentNullException("urlProviders");
//if there's already a singleton, and we're not replacing then there's no need to ensure anything
if (UmbracoContext.Current != null)
{
if (replaceContext == false)
@@ -132,6 +133,39 @@ namespace Umbraco.Web
UmbracoContext.Current._replacing = true;
}
var umbracoContext = CreateContext(httpContext, applicationContext, webSecurity, umbracoSettings, urlProviders, preview ?? false);
//assign the singleton
UmbracoContext.Current = umbracoContext;
return UmbracoContext.Current;
}
/// <summary>
/// Creates a standalone UmbracoContext instance
/// </summary>
/// <param name="httpContext"></param>
/// <param name="applicationContext"></param>
/// <param name="webSecurity"></param>
/// <param name="umbracoSettings"></param>
/// <param name="urlProviders"></param>
/// <param name="preview"></param>
/// <returns>
/// A new instance of UmbracoContext
/// </returns>
public static UmbracoContext CreateContext(
HttpContextBase httpContext,
ApplicationContext applicationContext,
WebSecurity webSecurity,
IUmbracoSettingsSection umbracoSettings,
IEnumerable<IUrlProvider> urlProviders,
bool preview)
{
if (httpContext == null) throw new ArgumentNullException("httpContext");
if (applicationContext == null) throw new ArgumentNullException("applicationContext");
if (webSecurity == null) throw new ArgumentNullException("webSecurity");
if (umbracoSettings == null) throw new ArgumentNullException("umbracoSettings");
if (urlProviders == null) throw new ArgumentNullException("urlProviders");
var umbracoContext = new UmbracoContext(
httpContext,
applicationContext,
@@ -142,15 +176,15 @@ namespace Umbraco.Web
// create the RoutingContext, and assign
var routingContext = new RoutingContext(
umbracoContext,
//TODO: Until the new cache is done we can't really expose these to override/mock
new Lazy<IEnumerable<IContentFinder>>(() => ContentFinderResolver.Current.Finders),
new Lazy<IContentFinder>(() => ContentLastChanceFinderResolver.Current.Finder),
// create the nice urls provider
// there's one per request because there are some behavior parameters that can be changed
new Lazy<UrlProvider>(
() => new UrlProvider(
() => new UrlProvider(
umbracoContext,
umbracoSettings.WebRouting,
urlProviders),
@@ -159,9 +193,7 @@ namespace Umbraco.Web
//assign the routing context back
umbracoContext.RoutingContext = routingContext;
//assign the singleton
UmbracoContext.Current = umbracoContext;
return UmbracoContext.Current;
return umbracoContext;
}
/// <summary>
@@ -460,18 +492,9 @@ namespace Umbraco.Web
protected override void DisposeResources()
{
Security.DisposeIfDisposable();
Security = null;
_umbracoContext = null;
//ensure not to dispose this!
Application = null;
//Before we set these to null but in fact these are application lifespan singletons so
//there's no reason we need to set them to null and this also caused a problem with packages
//trying to access the cache properties on RequestEnd.
//http://issues.umbraco.org/issue/U4-2734
//http://our.umbraco.org/projects/developer-tools/301-url-tracker/version-2/44327-Issues-with-URL-Tracker-in-614
//ContentCache = null;
//MediaCache = null;
//If not running in a web ctx, ensure the thread based instance is nulled
_umbracoContext = null;
}
}
}