<PackageReference Include="Castle.Windsor" Version="5.0.0" />

LifetimeScopeAccessor

using Castle.MicroKernel.Context; using Castle.MicroKernel.Lifestyle.Scoped; using System; namespace Castle.MicroKernel.Lifestyle { public class LifetimeScopeAccessor : IScopeAccessor, IDisposable { public void Dispose() { CallContextLifetimeScope.ObtainCurrentScope()?.Dispose(); } public ILifetimeScope GetScope(CreationContext context) { CallContextLifetimeScope callContextLifetimeScope = CallContextLifetimeScope.ObtainCurrentScope(); if (callContextLifetimeScope == null) throw new InvalidOperationException("Scope was not available. Did you forget to call container.BeginScope()?"); return callContextLifetimeScope; } } }