When I catch a .NET WebException
, should I close / dispose the Response.GetResponseStream()
?
The MSDN example does not close or dispose anything in the exception.
Many SO answers recommend disposing the response and / or the stream.
I disposed the stream and this caused big problems. Because GetResponseStream()
(always? / sometimes?) returns the same instance. So when I get the response stream and then dispose it, then maybe rethrow the exception to another layer that also gets the response stream it will be already disposed and unreadable and throw more exceptions because of that.
You should dispose of that stream because it might hold resources. But only dispose of it when you are done with it. Simply stop disposing it before you no longer need the stream. Make the last user of the stream dispose of it.
Probably, you should call GetResponseStream()
only once and pass the stream around explicitly so that it is clear that it's the same stream.