Don't throw ObjectDisposedException when disposing Observer for the first time - #219
Don't throw ObjectDisposedException when disposing Observer for the first time#219affederaffe wants to merge 2 commits into
Conversation
|
Note that by convention it's OK to call Dispose multiple times and it should never throw. I. e. Dispose is supposed to be a method that's always safe to call. From https://learn.microsoft.com/en-us/dotnet/api/system.idisposable.dispose?view=net-8.0:
|
|
Can you share some code that reproduces the problem? |
afaik Disposing an Observer never throws. It does call the observer's action handler with an ObjectDisposedException. |
|
In my opinion that's a bad design per convention it should not be an Exception. |
I agree that it may be non-intuitive that this is done with an exception. An other, more explicit parameter on the action delegate would have been clearer.
Depending on how you structure your code, it may be convenient to be able handle the end of the stream in the handler. That is the rationale for invoking the handler. I'll close this as I think there is meaning in notifying the observer the stream ended through disposal, and I don't want to introduce a breaking change. I'm open to an API addition which has the semantics you want. If you have a proposal, please create an issue for it. |
When disposing Observers, an ObjectDisposedException was thrown event though it was the first time disposing said observer. This PR fixes the behavior to only throw an exception when trying to dispose multiple times.