diff --git a/build/NuSpecs/UmbracoCms.Web.nuspec b/build/NuSpecs/UmbracoCms.Web.nuspec index 92cb0f065e..f12ada7e64 100644 --- a/build/NuSpecs/UmbracoCms.Web.nuspec +++ b/build/NuSpecs/UmbracoCms.Web.nuspec @@ -25,12 +25,12 @@ --> - + - + - + diff --git a/src/Umbraco.Core/IDisposeOnRequestEnd.cs b/src/Umbraco.Core/IDisposeOnRequestEnd.cs deleted file mode 100644 index 2515968b6d..0000000000 --- a/src/Umbraco.Core/IDisposeOnRequestEnd.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace Umbraco.Cms.Core -{ - /// - /// Any class implementing this interface that is added to the httpcontext.items keys or values will be disposed of at the end of the request. - /// - // TODO: Once UmbracoContext no longer needs this (see TODO in UmbracoContext), this should be killed - public interface IDisposeOnRequestEnd : IDisposable - { - } -} diff --git a/src/Umbraco.Web.Common/Cache/HttpContextRequestAppCache.cs b/src/Umbraco.Web.Common/Cache/HttpContextRequestAppCache.cs index 6d1b66c854..f43f9a9a24 100644 --- a/src/Umbraco.Web.Common/Cache/HttpContextRequestAppCache.cs +++ b/src/Umbraco.Web.Common/Cache/HttpContextRequestAppCache.cs @@ -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. /// /// - 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 logger = _httpContextAccessor.HttpContext?.RequestServices?.GetRequiredService>(); - foreach (KeyValuePair i in this) - { - // NOTE: All of these will be Lazy since that is how this cache works, - // but we'll include the 2nd check too - if (i.Value is Lazy 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); - } - } - } - } - /// /// Used as Scoped instance to allow locking within a request /// diff --git a/src/Umbraco.Web.Common/Middleware/UmbracoRequestMiddleware.cs b/src/Umbraco.Web.Common/Middleware/UmbracoRequestMiddleware.cs index 67bba794a7..02976f9e1d 100644 --- a/src/Umbraco.Web.Common/Middleware/UmbracoRequestMiddleware.cs +++ b/src/Umbraco.Web.Common/Middleware/UmbracoRequestMiddleware.cs @@ -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 } /// - /// 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. /// - private void DisposeRequestCacheItems(ILogger 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(); httpScopeReference.Register(); diff --git a/src/Umbraco.Web.Common/UmbracoContext/UmbracoContext.cs b/src/Umbraco.Web.Common/UmbracoContext/UmbracoContext.cs index 49a48a673d..50dcb4182c 100644 --- a/src/Umbraco.Web.Common/UmbracoContext/UmbracoContext.cs +++ b/src/Umbraco.Web.Common/UmbracoContext/UmbracoContext.cs @@ -14,10 +14,7 @@ namespace Umbraco.Cms.Web.Common.UmbracoContext /// /// Class that encapsulates Umbraco information of a specific HTTP request /// - // 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; diff --git a/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj b/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj index 72a63fa3a6..02a514e1ea 100644 --- a/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj +++ b/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj @@ -87,7 +87,7 @@ - + @@ -97,7 +97,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all - + 3.5.3 diff --git a/src/Umbraco.Web/UmbracoContext.cs b/src/Umbraco.Web/UmbracoContext.cs index 3b2af1e0ff..4aae46e44c 100644 --- a/src/Umbraco.Web/UmbracoContext.cs +++ b/src/Umbraco.Web/UmbracoContext.cs @@ -13,7 +13,7 @@ namespace Umbraco.Web { // NOTE: has all been ported to netcore but exists here just to keep the build working for tests - public class UmbracoContext : DisposableObjectSlim, IDisposeOnRequestEnd, IUmbracoContext + public class UmbracoContext : DisposableObjectSlim, IUmbracoContext { private readonly IHttpContextAccessor _httpContextAccessor; private readonly Lazy _publishedSnapshot;