IDisposable and UsingFactoryMethod
See original GitHub issueConsider:
public class ServiceInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Component.For<ISomeConnectionService>()
.ImplementedBy<SomeConnectionService>()
.LifestyleTransient()
.IsDefault());
container.Register(
Component.For<FailoverDatabaseConnectionExecutor>()
.ImplementedBy<FailoverDatabaseConnectionExecutor>()
.LifestyleTransient());
container.Register(Component.For<IDatabaseConnectionExecutor>()
.UsingFactoryMethod(CreateDatabaseConnectionExecutor)
.LifestyleTransient()
.IsDefault());
}
private static IDatabaseConnectionExecutor CreateDatabaseConnectionExecutor(IKernel kernel)
{
return kernel.Resolve<FailoverDatabaseConnectionExecutor>();
}
}
public interface IDatabaseConnectionExecutor
{
}
public class SomeConnectionService : ISomeConnectionService, System.IDisposable
{
public SomeConnectionService()
{
}
public void Dispose()
{
}
}
public interface ISomeConnectionService
{
}
public class FailoverDatabaseConnectionExecutor : IDatabaseConnectionExecutor
{
private readonly ISomeConnectionService _someConnectionService;
public FailoverDatabaseConnectionExecutor(ISomeConnectionService someConnectionService)
{
_someConnectionService = someConnectionService;
}
}
When attempting to inject ISomeConnectionService i receive the following error:
Instance FailoverDatabaseConnectionExecutor of component Late bound IDatabaseConnectionExecutor is already being tracked. The factory method providing instances of the component is reusing instances, but the lifestyle of the component is Transient which requires new instance each time. In most cases it is advised for the factory method not to be handling reuse of the instances, but to chose a lifestyle that does that appropriately. Alternatively, if you do not wish for Windsor to track the objects coming from the factory change your regustration to ‘.UsingFactoryMethod(yourFactory, managedExternally: true)’
If i remove IDisposable from SomeConnectionService then i don’t receive the error any more. Is this by design? Is it not possible to have an object implementing IDisposable in the dependency chain in conjunction with .UsingFactoryMethod? As a side note there is also a spelling mistake “regustration” in the message.
Issue Analytics
- State:
- Created 6 years ago
- Comments:16 (2 by maintainers)
Top GitHub Comments
@aaron-hammond - you are right.
From what I can see, the TransientLifestyleManager is asking the LifecycledComponentsReleasePolicy to track the FailoverDatabaseConnectionExecutor twice. I think this is happen because of the following resolution cycle:
Resolve<ISomeConnectionService> calls LifecycledComponentsReleasePolicy.Track<SomeConnectionService>()
Resolve<FailoverDatabaseConnectionExecutor> calls LifecycledComponentsReleasePolicy.Track<FailoverDatabaseConnectionExecutor>()
Resolve<IDatabaseConnectionExecutor> calls LifecycledComponentsReleasePolicy.Track<FailoverDatabaseConnectionExecutor>()
<- This fails because at the very end because it cannot add the same instance for FailoverDatabaseConnectionExecutor twice in the burden tracking for LifecycledComponentsReleasePolicy.This is a tricky one. I will keep looking.
@fir3pho3nixx Don’t feel bad. We’ve all been there! ❤️