question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IDisposable and UsingFactoryMethod

See original GitHub issue

Consider:

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:open
  • Created 6 years ago
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostcommented, Sep 13, 2017

@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.

0reactions
jnm2commented, Jan 13, 2018

@fir3pho3nixx Don’t feel bad. We’ve all been there! ❤️

Read more comments on GitHub >

github_iconTop Results From Across the Web

Castle Windsor UsingFactoryMethod with LifestyleTransient
When one of the objects implements IDisposable then this error occurs when used in conjunction with UsingFactoryMethod.
Read more >
How to implement IDisposable Design Pattern in C
We should use an IDisposable design pattern (or Dispose Pattern) when we need to dispose of unmanaged objects.
Read more >
Must I release everything when using Windsor?
But what happens with IDisposable components created through typed factories? Does the typed factory facility automatically create interceptors ...
Read more >
IDisposable Interface (System)
Provides a mechanism for releasing unmanaged resources.
Read more >
Wiring up TopShelf, Windsor and EasyNetQ
Windsor tracks any components that implement IDisposable and ... to call Dispose when the application shuts down, UsingFactoryMethod tells ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found