C#: "Using" Statements with HttpWebRequests/HttpWebResponses

30,573

Solution 1

HttpWebRequest itself is not disposable unlike HttpWebResponse. You should wrap disposable resources with using to allow early and determined cleanup. Correctly implemented IDisposable pattern allows multiple calls to Dispose without any issues so even the outer using statement wraps resource that during its own dispose disposes inner using statement resource it is still ok.

Code example

var request = (HttpWebRequest)WebRequest.Create("example.com"); 
using (var response = (HttpWebResponse)request.GetResponse()) 
{ 
    // Code here 
}

Solution 2

Everything wrapped in a using () {} block (that is, inside of the first brackets) is disposed when you leave the scope.

I haven't used your library so far (seems nice though), but I'd argue that you should explicitly dispose every IDisposable you create (= are responsible for) and don't return to a caller.

A sidenote, since I've seen a lot of people struggling with multiple things to dispose: Instead of

using (var foo = SomeIDisposable) {
  using (var bar = SomeOtherIDisposable) {
  }
}

which needs a lot of vertical space you can write

using (var foo = SomeIDisposable)
using (var bar = SomeOtherIDisposable) {
}

Solution 3

In order to prevent memory leaks you should call Dispose on every object that implements IDisposable. You can ensure that the Dispose method in called by using the using keyword (no pun intended) as it is just a syntactic sugar for try-finally block.

Share:
30,573
Maxim Zaslavsky
Author by

Maxim Zaslavsky

I'm a computer science student at Princeton University, originally from San Diego, CA. I use machine learning to approach problems in biology and healthcare. My current research applies machine learning to immunology. I wrote my first app (a VB6-powered word-search solver) when I was 7, and I've been hooked on programming ever since. I discovered and got involved in the Stack Overflow community in 2009 (when I was 12). Check out my website for more information or to get in touch.

Updated on April 06, 2020

Comments

  • Maxim Zaslavsky
    Maxim Zaslavsky about 4 years

    Jon Skeet made a comment (via Twitter) on my SOApiDotNet code (a .NET library for the pre-alpha Stack Overflow API):

    @maximz2005 One thing I've noticed just from browsing the source quickly: you don't disposed (sic) of WebResponses. "using" statements FTW.

    He indicates that I need to wrap these Web sessions in "using" statements. However, I have a question about this: should I wrap the whole thing, starting with the HttpWebRequest, or should I create the WebRequest outside of the "using" statement and then wrap the Response inside? I have a feeling that the difference is that, in the former, both objects would be disposed of - is this correct?

    Thanks in advance.

  • Maxim Zaslavsky
    Maxim Zaslavsky over 14 years
    So should I declare ..Request outside, or what?
  • Benjamin Podszun
    Benjamin Podszun over 14 years
    Yes, that means you'd do a var request = (HttpWebRequest)WebRequest.Create("example.com"); using (var response = (HttpWebResponse)request.GetResponse()) { // Code here }
  • Jan Jongboom
    Jan Jongboom over 14 years
    @Dzmitry, @Benjamin. I added Benjamin's code example to your answer.
  • Tomas
    Tomas over 14 years
    Your second paragraph (which is correct, I believe) contradicts the first. If everything inside the using-block is disposed, you wouldn't need the inner using statement.
  • Benjamin Podszun
    Benjamin Podszun over 14 years
    See my updated post: Everything inside of using (...) is disposed when you leave the following block (this part: { ... })