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.

Releasing resolved object on the constructor

See original GitHub issue

If we Resolve an object in the constructor, castle automatically releases the resolved object when parent object is released.

This test fails since MyDisposableClass is released by windsor.

    public class ConstructorResolveTests: TestBaseWithLocalIocManager
    {
        private readonly WindsorContainer _container;

        public ConstructorResolveTests()
        {
            _container = new WindsorContainer();

            _container.Register(
                Component.For<MyDisposableClass>().LifestyleTransient(),
                Component.For<MyClient>().LifestyleTransient()
                );
        }

        [Fact]
        public void Should_Not_Release_MyDisposableClass()
        {
            var client = _container.Resolve<MyClient>();
            _container.Release(client);

            MyDisposableClass.DisposeCount.ShouldBe(0); //But it is 1
        }

        public class MyClient
        {
            private readonly MyDisposableClass _myDisposableObject;

            public MyClient(IKernel kernel)
            {
                _myDisposableObject = kernel.Resolve<MyDisposableClass>();
            }
        }

        public class MyDisposableClass : IDisposable
        {
            public static int DisposeCount { get; set; }

            public void Dispose()
            {
                DisposeCount++;
            }
        }
    }

This is unexpected by me. Because, Resolving was done in a deeper level and I hardly find the actual reason. While this seems reasonable in some cases, it may not be proper always. Can we change this behaviour?

Thanks.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:18 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
IanYatescommented, Sep 22, 2015

“externally managed” is me misremembering the appropriate term 😃

Something like

container.Register( Component.For<ISomething>().UsingFactoryMethod(......, managedExternally: true) );

The … represents various overloads available for UsingFactoryMethod. All involve some kind of Func - one with no parameters, one that takes the kernel, another with the kernel & creationContext and another with even more parameters. Choose the appropriate one and set the managedExternally boolean true so that Windsor won’t track the component for release.

0reactions
hikalkancommented, Sep 20, 2017

Yes we can close. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve Javascript Promise outside the ...
You can wrap the Promise in a class. class Deferred { constructor(handler) { this. promise = new Promise((resolve, reject) => { this. reject...
Read more >
ASP.NET Core Dependency Injection Best Practices, Tips ...
Resolve dependencies in the service constructor if possible. Resolving in a service method makes your application more complicated and error ...
Read more >
ConstructorResolver
Object. Delegate for resolving constructors and factory methods. Performs constructor resolution through argument matching.
Read more >
Can Constructors Throw Exceptions in Java
The short answer is yes! Of course, properly implementing exceptions in your constructors is essential to getting the best results.
Read more >
Dependency Injection
It is constructor-injected with IIocResolver and uses it to resolve and release objects. There are a few overloads of the Resolve method which...
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