Skip to content

Commit

Permalink
Bugfix net6 ms di no available scope castleproject#662
Browse files Browse the repository at this point in the history
  • Loading branch information
weixiang-li committed Jan 17, 2024
1 parent 77216d1 commit 8eceee7
Show file tree
Hide file tree
Showing 8 changed files with 768 additions and 45 deletions.

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

1 comment on commit 8eceee7

@weixiang-li
Copy link
Owner Author

Choose a reason for hiding this comment

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

Please sign in to comment.