Releasing resolved object on the constructor
See original GitHub issueIf 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:
- Created 8 years ago
- Comments:18 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
“externally managed” is me misremembering the appropriate term 😃
Something like
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.
Yes we can close. Thanks.