2022-04-26 10:22:37 +01:00
|
|
|
// Copyright (c) Umbraco.
|
2021-03-05 15:27:45 +11:00
|
|
|
// 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
|
|
|
|
|
{
|
2022-04-26 10:22:37 +01:00
|
|
|
private readonly Infrastructure.Scoping.ScopeProvider _scopeProvider;
|
2021-03-05 15:27:45 +11:00
|
|
|
private bool _disposedValue;
|
|
|
|
|
private bool _registered = false;
|
|
|
|
|
|
2022-04-26 10:22:37 +01:00
|
|
|
public HttpScopeReference(Infrastructure.Scoping.ScopeProvider scopeProvider) => _scopeProvider = scopeProvider;
|
2021-03-05 15:27:45 +11:00
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (!_disposedValue)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
if (_registered)
|
|
|
|
|
{
|
|
|
|
|
// dispose the entire chain (if any)
|
|
|
|
|
// reset (don't commit by default)
|
2022-04-26 10:22:37 +01:00
|
|
|
Infrastructure.Scoping.Scope? scope;
|
2021-03-05 15:27:45 +11:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|