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

ScopeCache

using Castle.Core.Internal; using System; using System.Collections.Generic; using System.Linq; using System.Threading; namespace Castle.MicroKernel.Lifestyle.Scoped { public class ScopeCache : IScopeCache, IDisposable { private IDictionary<object, Burden> cache = new Dictionary<object, Burden>(); public Burden this[object id] { get { try { cache.TryGetValue(id, out Burden value); return value; } catch (NullReferenceException) { throw new ObjectDisposedException("Scope cache was already disposed. This is most likely a bug in the calling code."); } } set { try { cache.Add(id, value); } catch (NullReferenceException) { throw new ObjectDisposedException("Scope cache was already disposed. This is most likely a bug in the calling code."); } } } public void Dispose() { IDictionary<object, Burden> dictionary = Interlocked.Exchange(ref cache, null); if (dictionary != null) dictionary.Values.Reverse().ForEach(delegate(Burden b) { b.Release(); }); } } }