Updates scopes in execution context to use a Stack so we know which is the top Scope/Context. Fixes disposing things on end request, fixes ensuring orphaned scopes are disposed at end request.

This commit is contained in:
Shannon
2021-03-05 15:27:45 +11:00
parent 7d45a585ee
commit df333ec8cb
13 changed files with 498 additions and 294 deletions

View File

@@ -0,0 +1,48 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
namespace Umbraco.Cms.Core.Scoping
{
/// <summary>
/// Disposed at the end of the request to cleanup any orphaned Scopes.
/// </summary>
/// <remarks>Registered as Scoped in DI (per request)</remarks>
internal class HttpScopeReference : IHttpScopeReference
{
private readonly ScopeProvider _scopeProvider;
private bool _disposedValue;
private bool _registered = false;
public HttpScopeReference(ScopeProvider scopeProvider) => _scopeProvider = scopeProvider;
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
if (_registered)
{
// dispose the entire chain (if any)
// reset (don't commit by default)
Scope scope;
while ((scope = _scopeProvider.AmbientScope) != null)
{
scope.Reset();
scope.Dispose();
}
}
}
_disposedValue = true;
}
}
public void Dispose() =>
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
public void Register() => _registered = true;
}
}