diff --git a/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerScope.cs b/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerScope.cs index 2ff5a5c49..8fc610fc1 100644 --- a/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerScope.cs +++ b/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerScope.cs @@ -35,9 +35,9 @@ internal static ExtensionContainerScopeBase BeginScope() public override void Dispose() { - if (ExtensionContainerScopeCache.current.Value == this) + if (ExtensionContainerScopeCache.TryCurrent == this) { - ExtensionContainerScopeCache.current.Value = parent; + ExtensionContainerScopeCache.Current = parent; } base.Dispose(); } diff --git a/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerScopeCache.cs b/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerScopeCache.cs index d89f9dd1f..39ad88b36 100644 --- a/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerScopeCache.cs +++ b/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerScopeCache.cs @@ -19,7 +19,7 @@ namespace Castle.Windsor.Extensions.DependencyInjection.Scope internal static class ExtensionContainerScopeCache { - internal static readonly AsyncLocal current = new AsyncLocal(); + private static readonly AsyncLocal current = new AsyncLocal(); /// Current scope for the thread. Initial scope will be set when calling BeginRootScope from a ExtensionContainerRootScope instance. /// Thrown when there is no scope available. internal static ExtensionContainerScopeBase Current @@ -27,5 +27,7 @@ internal static ExtensionContainerScopeBase Current get => current.Value ?? throw new InvalidOperationException("No scope available"); set => current.Value = value; } + + internal static ExtensionContainerScopeBase TryCurrent => current.Value; } } \ No newline at end of file diff --git a/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ForcedScope.cs b/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ForcedScope.cs index c9d41dfa6..c4fb8fe5e 100644 --- a/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ForcedScope.cs +++ b/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ForcedScope.cs @@ -23,9 +23,9 @@ internal class ForcedScope : IDisposable { private readonly ExtensionContainerScopeBase scope; private readonly ExtensionContainerScopeBase previousScope; - internal ForcedScope(ExtensionContainerScopeBase scope) + internal ForcedScope(ExtensionContainerScopeBase scope, ExtensionContainerRootScope rootScope) { - previousScope = ExtensionContainerScopeCache.Current; + previousScope = ExtensionContainerScopeCache.TryCurrent ?? rootScope; this.scope = scope; ExtensionContainerScopeCache.Current = scope; } diff --git a/src/Castle.Windsor.Extensions.DependencyInjection/WindsorScopedServiceProvider.cs b/src/Castle.Windsor.Extensions.DependencyInjection/WindsorScopedServiceProvider.cs index 4e61f2f76..6e80f8792 100644 --- a/src/Castle.Windsor.Extensions.DependencyInjection/WindsorScopedServiceProvider.cs +++ b/src/Castle.Windsor.Extensions.DependencyInjection/WindsorScopedServiceProvider.cs @@ -30,16 +30,19 @@ internal class WindsorScopedServiceProvider : IServiceProvider, ISupportRequired private bool disposing; private readonly IWindsorContainer container; + + private readonly ExtensionContainerRootScope rootScope; - public WindsorScopedServiceProvider(IWindsorContainer container) + public WindsorScopedServiceProvider(IWindsorContainer container, ExtensionContainerRootScope rootScope) { this.container = container; - scope = ExtensionContainerScopeCache.Current; + this.scope = ExtensionContainerScopeCache.Current; + this.rootScope = rootScope; } public object GetService(Type serviceType) { - using(_ = new ForcedScope(scope)) + using(_ = new ForcedScope(scope, rootScope)) { return ResolveInstanceOrNull(serviceType, true); } @@ -47,7 +50,7 @@ public object GetService(Type serviceType) public object GetRequiredService(Type serviceType) { - using(_ = new ForcedScope(scope)) + using(_ = new ForcedScope(scope, rootScope)) { return ResolveInstanceOrNull(serviceType, false); } diff --git a/src/Castle.Windsor.Extensions.DependencyInjection/WindsorServiceProviderFactoryBase.cs b/src/Castle.Windsor.Extensions.DependencyInjection/WindsorServiceProviderFactoryBase.cs index 21363c4fd..a51f1f780 100644 --- a/src/Castle.Windsor.Extensions.DependencyInjection/WindsorServiceProviderFactoryBase.cs +++ b/src/Castle.Windsor.Extensions.DependencyInjection/WindsorServiceProviderFactoryBase.cs @@ -103,6 +103,7 @@ protected virtual void RegisterProviders(IWindsorContainer container) container.Register(Component .For() .ImplementedBy() + .DependsOn(Dependency.OnValue(rootScope)) .LifeStyle.ScopedToNetServiceScope()); }