Should WebException.Response.GetResponseStream () be private / remote?

When I catch .NET WebException , should I close / delete Response.GetResponseStream() ?

The MSDN example does not close or leave anything in this exception.

Many https://stackoverflow.com/a/168666/ ... answers recommend deleting the answer and / or stream.

I chose the thread and it caused big problems. Because GetResponseStream() (always? / Sometimes?) Returns the same instance. Therefore, when I receive the response stream and then delete it, it is possible to rearrange the exception to a different level, which will also receive the response stream, it will already be deleted and unreadable, and more exceptions will come out of this.

+6
source share
2 answers

You must get rid of this thread because it may contain resources. But delete it only when you are done with it. Just stop using it before you need the stream anymore. Make it the last user of the stream.

You should GetResponseStream() call GetResponseStream() only once and pass the stream explicitly so that it is clear that it is the same stream.

+2
source

The short answer is that you do not need to dispose of it, although it is a good exercise to place any IDisposable objects that you own.

In fact, trying to remove WebException.Response or the thread returned from it will cause the problems you mentioned, as you may encounter code trying to read its properties in the top call chain exception handler.

The reason it is safe not to dispose of it is because HttpWebRequest internally creates a memory stream from the network stream before it throws a WebException and the main network stream is already closed / deleted. Thus, at the moment he does not have an unmanaged resource. I assume this solution will facilitate exception handling.

Unfortunately, the MSDN documentation has no explanation for this. Technically, the implementation may change in the future, creating a problem for your code, not eliminating the HttpWebResponse and / or the associated stream that you receive from WebException , but it is unlikely that this behavior would change the implementation, given that many applications depend on the current behavior.

I must add that good practice is that you should dispose of IDisposable objects that you own. If possible, use the HttpClient class so you don't have to deal with this situation at all. If you cannot, consider handling WebException yourself and throw a new type of exception that will not WebException caller of your code so that you do not encounter a situation where the caller tries to access WebException.Response after removing it.

Disclaimer: I work at Microsoft, but this does not reflect the opinion of my employer or the .NET Framework. No warranty is implied.

+5
source

All Articles