Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Root scope when current is null (fixes #646) #670

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ namespace Castle.Windsor.Extensions.DependencyInjection.Scope

internal static class ExtensionContainerScopeCache
{
internal static readonly AsyncLocal<ExtensionContainerScopeBase> current = new AsyncLocal<ExtensionContainerScopeBase>();
private static readonly AsyncLocal<ExtensionContainerScopeBase> current = new AsyncLocal<ExtensionContainerScopeBase>();
/// <summary>Current scope for the thread. Initial scope will be set when calling BeginRootScope from a ExtensionContainerRootScope instance.</summary>
/// <exception cref="InvalidOperationException">Thrown when there is no scope available.</exception>
internal static ExtensionContainerScopeBase Current
{
get => current.Value ?? throw new InvalidOperationException("No scope available");
set => current.Value = value;
}

internal static ExtensionContainerScopeBase TryCurrent => current.Value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TryCurrent is a strange name, it's not a function. Any reason not to just name it Current and just document that is can return null?

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,27 @@ 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);
}
}

public object GetRequiredService(Type serviceType)
{
using(_ = new ForcedScope(scope))
using(_ = new ForcedScope(scope, rootScope))
{
return ResolveInstanceOrNull(serviceType, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ protected virtual void RegisterProviders(IWindsorContainer container)
container.Register(Component
.For<IServiceProvider, ISupportRequiredService>()
.ImplementedBy<WindsorScopedServiceProvider>()
.DependsOn(Dependency.OnValue<ExtensionContainerRootScope>(rootScope))
.LifeStyle.ScopedToNetServiceScope());
}

Expand Down