diff --git a/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerRootScopeAccessor.cs b/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerRootScopeAccessor.cs index a6ddf22d1f..8779538f89 100644 --- a/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerRootScopeAccessor.cs +++ b/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerRootScopeAccessor.cs @@ -23,7 +23,7 @@ internal class ExtensionContainerRootScopeAccessor : IScopeAccessor { public ILifetimeScope GetScope(CreationContext context) { - return ExtensionContainerScope.Current?.RootScope ?? throw new InvalidOperationException("No root scope"); + return ExtensionContainerScope.CurrentOrThrow.RootScope ?? throw new InvalidOperationException("No root scope"); } public void Dispose() diff --git a/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerScope.cs b/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerScope.cs index f87acd55f0..c165b515f6 100644 --- a/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerScope.cs +++ b/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerScope.cs @@ -23,13 +23,13 @@ namespace Castle.Windsor.Extensions.DependencyInjection.Scope internal class ExtensionContainerScope : ILifetimeScope, IDisposable { - internal static ExtensionContainerScope Current + internal static ExtensionContainerScope CurrentOrThrow { - get => current.Value; + get => current.Value ?? throw new InvalidOperationException("No scope available"); set => current.Value = value; } public static string TransientMarker = "Transient"; - protected static readonly AsyncLocal current = new AsyncLocal(); + protected static readonly AsyncLocal current = new AsyncLocal(); private readonly ExtensionContainerScope parent; private readonly IScopeCache scopeCache; @@ -44,8 +44,7 @@ protected ExtensionContainerScope(ExtensionContainerScope parent, ExtensionConta internal static ExtensionContainerScope BeginScope() { - var parent = Current; - if (parent == null) throw new ArgumentNullException($"There is no parent scope to allow for BeginScope"); + var parent = CurrentOrThrow; var scope = new ExtensionContainerScope(parent, parent.RootScope); current.Value = scope; return scope; diff --git a/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerScopeAccessor.cs b/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerScopeAccessor.cs index 087ed7e3d4..8f568ae123 100644 --- a/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerScopeAccessor.cs +++ b/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ExtensionContainerScopeAccessor.cs @@ -23,7 +23,7 @@ internal class ExtensionContainerScopeAccessor : IScopeAccessor { public ILifetimeScope GetScope(CreationContext context) { - return ExtensionContainerScope.Current ?? throw new InvalidOperationException("No scope available"); + return ExtensionContainerScope.CurrentOrThrow ?? throw new InvalidOperationException("No scope available"); } public void Dispose() diff --git a/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ForcedScope.cs b/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ForcedScope.cs index f35a03af5b..ce5213f9dd 100644 --- a/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ForcedScope.cs +++ b/src/Castle.Windsor.Extensions.DependencyInjection/Scope/ForcedScope.cs @@ -24,12 +24,12 @@ internal class ForcedScope : IDisposable private readonly ExtensionContainerScope previousScope; internal ForcedScope(ExtensionContainerScope scope) { - previousScope = ExtensionContainerScope.Current; - ExtensionContainerScope.Current = scope; + previousScope = ExtensionContainerScope.CurrentOrThrow; + ExtensionContainerScope.CurrentOrThrow = scope; } public void Dispose() { - ExtensionContainerScope.Current = previousScope; + ExtensionContainerScope.CurrentOrThrow = previousScope; } } } \ No newline at end of file