Skip to content

Commit

Permalink
Added comments on WindsorScopedServiceProvider dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
AGiorgetti authored and alkampfergit committed Feb 9, 2024
1 parent b390405 commit b997da0
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Castle.Windsor.Extensions.DependencyInjection
using System;
using System.Collections.Generic;
using System.Reflection;

using Castle.Windsor;
using Castle.Windsor.Extensions.DependencyInjection.Scope;

Expand All @@ -30,7 +30,7 @@ internal class WindsorScopedServiceProvider : IServiceProvider, ISupportRequired
private bool disposing;

private readonly IWindsorContainer container;

public WindsorScopedServiceProvider(IWindsorContainer container)
{
this.container = container;
Expand All @@ -39,27 +39,30 @@ public WindsorScopedServiceProvider(IWindsorContainer container)

public object GetService(Type serviceType)
{
using(_ = new ForcedScope(scope))
using (_ = new ForcedScope(scope))
{
return ResolveInstanceOrNull(serviceType, true);
return ResolveInstanceOrNull(serviceType, true);
}
}

public object GetRequiredService(Type serviceType)
{
using(_ = new ForcedScope(scope))
using (_ = new ForcedScope(scope))
{
return ResolveInstanceOrNull(serviceType, false);
return ResolveInstanceOrNull(serviceType, false);
}
}

public void Dispose()
{
// root scope should be tied to the root IserviceProvider, so
// it has to be disposed with the IserviceProvider to which is tied to
if (!(scope is ExtensionContainerRootScope)) return;
if (disposing) return;
disposing = true;
var disposableScope = scope as IDisposable;
disposableScope?.Dispose();
// disping the container here is questionable... what if I want to create another IServiceProvider form the factory?
container.Dispose();
}
private object ResolveInstanceOrNull(Type serviceType, bool isOptional)
Expand Down

0 comments on commit b997da0

Please sign in to comment.