Netcore: Replace IUmbracoRequestLifetime with EventAggregator nofitications (#9711)

* AB#9928 - Replace IUmbracoRequestLifetime with event aggregator

* AB#9928 - Fix issue with profiling, by calling it explicitly in the request middleware

* Longer timeout to avoid flaky test

* Longer timeout to avoid flaky test

Co-authored-by: Elitsa Marinovska <elm@umbraco.dk>
This commit is contained in:
Bjarke Berg
2021-01-26 07:58:11 +01:00
committed by GitHub
parent c79b31ed2e
commit 6f357a771d
14 changed files with 113 additions and 128 deletions

View File

@@ -7,9 +7,10 @@ using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.Logging;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Events;
using Umbraco.Core.Logging;
using Umbraco.Extensions;
using Umbraco.Web.Common.Lifetime;
using Umbraco.Web.Common.Profiler;
using Umbraco.Web.PublishedCache.NuCache;
namespace Umbraco.Web.Common.Middleware
@@ -29,11 +30,13 @@ namespace Umbraco.Web.Common.Middleware
public class UmbracoRequestMiddleware : IMiddleware
{
private readonly ILogger<UmbracoRequestMiddleware> _logger;
private readonly IUmbracoRequestLifetimeManager _umbracoRequestLifetimeManager;
private readonly IUmbracoContextFactory _umbracoContextFactory;
private readonly IRequestCache _requestCache;
private readonly IBackOfficeSecurityFactory _backofficeSecurityFactory;
private readonly PublishedSnapshotServiceEventHandler _publishedSnapshotServiceEventHandler;
private readonly IEventAggregator _eventAggregator;
private readonly WebProfiler _profiler;
private static bool s_cacheInitialized = false;
private static bool s_cacheInitializedFlag = false;
private static object s_cacheInitializedLock = new object();
@@ -43,18 +46,20 @@ namespace Umbraco.Web.Common.Middleware
/// </summary>
public UmbracoRequestMiddleware(
ILogger<UmbracoRequestMiddleware> logger,
IUmbracoRequestLifetimeManager umbracoRequestLifetimeManager,
IUmbracoContextFactory umbracoContextFactory,
IRequestCache requestCache,
IBackOfficeSecurityFactory backofficeSecurityFactory,
PublishedSnapshotServiceEventHandler publishedSnapshotServiceEventHandler)
PublishedSnapshotServiceEventHandler publishedSnapshotServiceEventHandler,
IEventAggregator eventAggregator,
IProfiler profiler)
{
_logger = logger;
_umbracoRequestLifetimeManager = umbracoRequestLifetimeManager;
_umbracoContextFactory = umbracoContextFactory;
_requestCache = requestCache;
_backofficeSecurityFactory = backofficeSecurityFactory;
_publishedSnapshotServiceEventHandler = publishedSnapshotServiceEventHandler;
_eventAggregator = eventAggregator;
_profiler = profiler as WebProfiler; // Ignore if not a WebProfiler
}
/// <inheritdoc/>
@@ -67,6 +72,10 @@ namespace Umbraco.Web.Common.Middleware
return;
}
// Profiling start needs to be one of the first things that happens.
// Also MiniProfiler.Current becomes null if it is handled by the event aggregator due to async/await
_profiler?.UmbracoApplicationBeginRequest(context);
EnsureContentCacheInitialized();
_backofficeSecurityFactory.EnsureBackOfficeSecurity(); // Needs to be before UmbracoContext, TODO: Why?
@@ -86,7 +95,7 @@ namespace Umbraco.Web.Common.Middleware
try
{
_umbracoRequestLifetimeManager.InitRequest(context);
await _eventAggregator.PublishAsync(new UmbracoRequestBegin(context));
}
catch (Exception ex)
{
@@ -98,10 +107,11 @@ namespace Umbraco.Web.Common.Middleware
try
{
await next(context);
}
finally
{
_umbracoRequestLifetimeManager.EndRequest(context);
await _eventAggregator.PublishAsync(new UmbracoRequestEnd(context));
}
}
}
@@ -122,6 +132,10 @@ namespace Umbraco.Web.Common.Middleware
umbracoContextReference.Dispose();
}
}
// Profiling end needs to be last of the first things that happens.
// Also MiniProfiler.Current becomes null if it is handled by the event aggregator due to async/await
_profiler?.UmbracoApplicationEndRequest(context);
}
/// <summary>