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.

Typed factory should allow disposing multiple times

See original GitHub issue

According 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:

https://github.com/castleproject/Windsor/blob/603d78d491790c88cff604051c92115e8bb5d9f9/src/Castle.Windsor/Facilities/TypedFactory/Internal/TypedFactoryInterceptor.cs#L55

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jonorossicommented, Sep 19, 2019

Therefore, I’d say that not only the second call to Dispose should be ignored by this interceptor, but also calls to Release method as well after the factory had been disposed.

I can make a PR with the changes and some tests if needed.

@ivan-danilov makes sense. I think I was misunderstanding the situation like @mjacobs80 also has:

Basically the order the components are being disposed is not guaranteed hence Dispose being called twice on the factory.

0reactions
mjacobs80commented, Sep 18, 2019

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 inherits IDisposable.

I have a component that implements IDisposable, it has an interface based TypedFactory injected in the constructor and it’s Dispose is called in the components Dispose.

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 hence Dispose 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 a Transient calls it’s Dispose (if implemented) immediately. I’ve not tested what happens if we don’t Release explicitly before calling Dispose 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 or PerWcfOperation 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 from Dispose 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-what

TypedFactory 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 a try catch around it to deal with it robustly in situations where we’d want a component to be a Singleton.

Read more comments on GitHub >

github_iconTop 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 >

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