Typed factory should allow disposing multiple times
See original GitHub issueAccording to .NET Guidelines a dispose method should tolerate being called multiple times without throwing an exception.
This exception should not be thrown when method
equals FactoryMethod.Dispose
:
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Should IDisposable.Dispose() be made safe to call multiple ...
You should be safe to call it more than once, though you should probably avoid it if you can. From the MSDN page...
Read more >CA2202: Do not dispose objects multiple times
To fix a violation of this rule, change the implementation so that regardless of the code path, Dispose is called only one time...
Read more >From Dispose Pattern to Auto-disposable Objects in .NET
Once we are done processing data, we need to dispose objects in the reverse order of their construction. This is important, because the...
Read more >HttpClient Creation and Disposal Internals ... - Steve Gordon
In this post I answer the question of whether we need to dispose of HttpClient instances and look at this in relation to...
Read more >IDisposable Interface in C# | Using-Dispose Pattern
If an object's Dispose() method is called more than once, the object must ignore all calls after the first one and it must...
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
@ivan-danilov makes sense. I think I was misunderstanding the situation like @mjacobs80 also has:
I just fell into this and it looks like no progress has been made on this one. Following the documentation about the
TypedFactory
(https://github.com/castleproject/Windsor/blob/master/docs/typed-factory-facility-interface-based.md) I’ve created a factory interface that inheritsIDisposable
.I have a component that implements
IDisposable
, it has an interface basedTypedFactory
injected in the constructor and it’sDispose
is called in the componentsDispose
.Reviewing the stack trace the exception happens when I’ve called
Dispose
on the container itself which looks to match what @ivan-danilov encountered. Basically the order the components are being disposed is not guaranteed henceDispose
being called twice on the factory.Playing with lifestyles I can confirm that If the component that uses the factory is
Transient
it doesn’t have this issue as a release on aTransient
calls it’sDispose
(if implemented) immediately. I’ve not tested what happens if we don’tRelease
explicitly before callingDispose
on the container.I’ve got to do some further work checking behaviour under WCF as the component in this case is the service implementation which will be
PerWbRequest
orPerWcfOperation
in many cases. I simply encountered this during unit testing where we’ve registered using the default lifestyle.This all said throwing an exception from a
Dispose
is not expected. As @jonyadamit pointed out, throwing exceptions fromDispose
is not in accordance with .NET guidance about allowing dispose to be called multiple times, here is a link about this from plurasight https://www.pluralsight.com/blog/software-development/idisposable-for-dummies-1-why-whatTypedFactory
doesn’t expose a property to check if it is already disposed, and I don’t want to roll my own implementation on the interface as I want interface based typed factories. This appears to leave the only option to remember to put atry catch
around it to deal with it robustly in situations where we’d want a component to be aSingleton
.