diff --git a/src/SimpleInjector/ScopedLifestyle.cs b/src/SimpleInjector/ScopedLifestyle.cs index 58ca47d8..93f18389 100644 --- a/src/SimpleInjector/ScopedLifestyle.cs +++ b/src/SimpleInjector/ScopedLifestyle.cs @@ -4,6 +4,7 @@ namespace SimpleInjector { using System; + using System.Runtime.CompilerServices; using SimpleInjector.Lifestyles; /// @@ -93,14 +94,14 @@ public void RegisterForDisposal(Container container, IDisposable disposable) } /// - /// Sets the given as current scope in the given context. + /// Sets the given as current scope in the given context. An existing scope + /// will be overridden and not disposed of. If the overridden scope must be disposed of, this + /// must be done manually. /// /// The current scope. /// Thrown when is a null reference. /// Thrown when the is not related to /// a . - /// Thrown when there is already an active scope in the - /// current context. /// Thrown when the implementation does not support setting /// the current scope. public void SetCurrentScope(Scope scope) @@ -112,12 +113,6 @@ public void SetCurrentScope(Scope scope) throw new ArgumentException("The scope has no related Container.", nameof(scope)); } - if (this.GetCurrentScope(scope.Container) != null) - { - throw new InvalidOperationException( - $"This method can only be called in case {nameof(GetCurrentScope)} returns null."); - } - this.SetCurrentScopeCore(scope); } @@ -180,7 +175,7 @@ protected virtual void SetCurrentScopeCore(Scope scope) $"({this.GetType().ToFriendlyName()}."); } - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] private Scope GetCurrentScopeOrThrow(Container container) { Scope? scope = this.GetCurrentScopeInternal(container); @@ -193,7 +188,7 @@ private Scope GetCurrentScopeOrThrow(Container container) return scope!; } - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] private Scope? GetCurrentScopeInternal(Container container) { // If we are running verification in the current thread, we prefer returning a verification scope