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

Bugfix net6 ms di no available scope #662 #666

Open
wants to merge 1 commit 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

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class ExtensionContainerRootScopeAccessor : IScopeAccessor
{
public ILifetimeScope GetScope(CreationContext context)
{
return ExtensionContainerScopeCache.Current.RootScope ?? throw new InvalidOperationException("No root scope available");
return ExtensionContainerScopeCache.current.Value?.RootScope ?? throw new InvalidOperationException("No root scope available");
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ internal class ExtensionContainerScope : ExtensionContainerScopeBase

protected ExtensionContainerScope()
{
parent = ExtensionContainerScopeCache.Current;
parent = ExtensionContainerScopeCache.current.Value;
}

internal override ExtensionContainerScopeBase RootScope { get; set; }


internal static ExtensionContainerScopeBase BeginScope()
internal static ExtensionContainerScopeBase BeginScope(ExtensionContainerScopeBase rootScope = null)
{
var scope = new ExtensionContainerScope { RootScope = ExtensionContainerScopeCache.Current.RootScope };
var scope = new ExtensionContainerScope { RootScope = rootScope };
ExtensionContainerScopeCache.Current = scope;
return scope;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ internal class ForcedScope : IDisposable
private readonly ExtensionContainerScopeBase previousScope;
internal ForcedScope(ExtensionContainerScopeBase scope)
{
previousScope = ExtensionContainerScopeCache.Current;
previousScope = ExtensionContainerScopeCache.current.Value;
this.scope = scope;
ExtensionContainerScopeCache.Current = scope;
}
public void Dispose()
{
if(ExtensionContainerScopeCache.Current != scope) return;
if(ExtensionContainerScopeCache.Current != scope || previousScope == null) return;
ExtensionContainerScopeCache.Current = previousScope;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ namespace Castle.Windsor.Extensions.DependencyInjection.Scope
internal class WindsorScopeFactory : IServiceScopeFactory
{
private readonly IWindsorContainer scopeFactoryContainer;

public WindsorScopeFactory(IWindsorContainer container)
private readonly ExtensionContainerScopeBase rootScope;

public WindsorScopeFactory(IWindsorContainer container, ExtensionContainerScopeBase rootScope)
{
scopeFactoryContainer = container;
this.rootScope = rootScope;
}

public IServiceScope CreateScope()
{
var scope = ExtensionContainerScope.BeginScope();
var scope = ExtensionContainerScope.BeginScope(rootScope);

//since WindsorServiceProvider is scoped, this gives us new instance
var provider = scopeFactoryContainer.Resolve<IServiceProvider>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public object GetRequiredService(Type serviceType)

public void Dispose()
{
if (!(scope is ExtensionContainerRootScope)) return;
if (scope != scope.RootScope) return;
if (disposing) return;
disposing = true;
var disposableScope = scope as IDisposable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Castle.Windsor.Extensions.DependencyInjection

public abstract class WindsorServiceProviderFactoryBase : IServiceProviderFactory<IWindsorContainer>
{
internal ExtensionContainerRootScope rootScope;
internal ExtensionContainerScopeBase rootScope;
protected IWindsorContainer rootContainer;

public virtual IWindsorContainer Container => rootContainer;
Expand All @@ -44,7 +44,8 @@ public virtual IServiceProvider CreateServiceProvider(IWindsorContainer containe

protected virtual void CreateRootScope()
{
rootScope = ExtensionContainerRootScope.BeginRootScope();
rootScope = ExtensionContainerScope.BeginScope();
rootScope.RootScope = rootScope;
}

protected virtual void CreateRootContainer()
Expand Down Expand Up @@ -111,7 +112,7 @@ protected virtual void RegisterFactories(IWindsorContainer container)
container.Register(Component
.For<IServiceScopeFactory>()
.ImplementedBy<WindsorScopeFactory>()
.DependsOn(Dependency.OnValue<ExtensionContainerRootScope>(rootScope))
.DependsOn(Dependency.OnValue<ExtensionContainerScopeBase>(rootScope))
.LifestyleSingleton(),
Component
.For<IServiceProviderFactory<IWindsorContainer>>()
Expand Down