Fixup package refs and nuspecs, remove IDisposeOnRequestEnd

This commit is contained in:
Shannon
2021-03-09 13:12:52 +11:00
parent 6dd5714923
commit a668e9aa07
7 changed files with 21 additions and 70 deletions

View File

@@ -5,8 +5,6 @@ using System.Linq;
using System.Threading;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Events;
using Umbraco.Extensions;
@@ -21,7 +19,7 @@ namespace Umbraco.Cms.Core.Cache
/// in order to facilitate the correct locking and releasing allocations.
/// </para>
/// </remarks>
public class HttpContextRequestAppCache : FastDictionaryAppCacheBase, IRequestCache, IDisposable
public class HttpContextRequestAppCache : FastDictionaryAppCacheBase, IRequestCache
{
private readonly IHttpContextAccessor _httpContextAccessor;
@@ -80,7 +78,10 @@ namespace Umbraco.Cms.Core.Cache
var value = result.Value; // will not throw (safe lazy)
if (value is SafeLazy.ExceptionHolder eh)
{
eh.Exception.Throw(); // throw once!
}
return value;
}
@@ -88,7 +89,10 @@ namespace Umbraco.Cms.Core.Cache
{
//no place to cache so just return the callback result
if (!TryGetContextItems(out var items))
{
return false;
}
key = GetCacheKey(key);
try
{
@@ -107,7 +111,10 @@ namespace Umbraco.Cms.Core.Cache
{
//no place to cache so just return the callback result
if (!TryGetContextItems(out var items))
{
return false;
}
key = GetCacheKey(key);
try
{
@@ -243,41 +250,6 @@ namespace Umbraco.Cms.Core.Cache
}
}
// This is not a typical dispose pattern since this can be called multiple times to dispose
// whatever might be in the current context.
public void Dispose()
{
// need to resolve from request services since IRequestCache is a non DI service and we don't have a logger when created
ILogger<HttpContextRequestAppCache> logger = _httpContextAccessor.HttpContext?.RequestServices?.GetRequiredService<ILogger<HttpContextRequestAppCache>>();
foreach (KeyValuePair<string, object> i in this)
{
// NOTE: All of these will be Lazy<object> since that is how this cache works,
// but we'll include the 2nd check too
if (i.Value is Lazy<object> lazy && lazy.IsValueCreated && lazy.Value is IDisposeOnRequestEnd doer1)
{
try
{
doer1.Dispose();
}
catch (Exception ex)
{
logger.LogError("Could not dispose item with key " + i.Key, ex);
}
}
else if (i.Value is IDisposeOnRequestEnd doer2)
{
try
{
doer2.Dispose();
}
catch (Exception ex)
{
logger.LogError("Could not dispose item with key " + i.Key, ex);
}
}
}
}
/// <summary>
/// Used as Scoped instance to allow locking within a request
/// </summary>

View File

@@ -127,10 +127,11 @@ namespace Umbraco.Cms.Web.Common.Middleware
try
{
DisposeRequestCacheItems(_logger, _requestCache, context.Request);
DisposeHttpContextItems(context.Request);
}
finally
{
// Dispose the umbraco context reference which will in turn dispose the UmbracoContext itself.
umbracoContextReference.Dispose();
}
}
@@ -153,9 +154,9 @@ namespace Umbraco.Cms.Web.Common.Middleware
}
/// <summary>
/// Any object that is in the HttpContext.Items collection that is IDisposable will get disposed on the end of the request
/// Dispose some request scoped objects that we are maintaining the lifecycle for.
/// </summary>
private void DisposeRequestCacheItems(ILogger<UmbracoRequestMiddleware> logger, IRequestCache requestCache, HttpRequest request)
private void DisposeHttpContextItems(HttpRequest request)
{
// do not process if client-side request
if (request.IsClientSideRequest())
@@ -163,13 +164,6 @@ namespace Umbraco.Cms.Web.Common.Middleware
return;
}
// dispose the request cache at the end of the request
// and it can take care of disposing it's items if there are any
if (requestCache is IDisposable rd)
{
rd.Dispose();
}
// ensure this is disposed by DI at the end of the request
IHttpScopeReference httpScopeReference = request.HttpContext.RequestServices.GetRequiredService<IHttpScopeReference>();
httpScopeReference.Register();

View File

@@ -14,10 +14,7 @@ namespace Umbraco.Cms.Web.Common.UmbracoContext
/// <summary>
/// Class that encapsulates Umbraco information of a specific HTTP request
/// </summary>
// TODO: When https://github.com/umbraco/Umbraco-CMS/pull/9916 is merged, remove IDisposeOnRequestEnd
// and just explicitly register the created UmbracoContext with being disposed on end request with
// the HttpContext.Response
public class UmbracoContext : DisposableObjectSlim, IDisposeOnRequestEnd, IUmbracoContext
public class UmbracoContext : DisposableObjectSlim, IUmbracoContext
{
private readonly IHostingEnvironment _hostingEnvironment;
private readonly UriUtility _uriUtility;