18

I have some code like, this code is used very heavily:

using (HttpWebResponse r = _GetHttpWebResponse(uri, body, method, contentType, headers)) {
    /* do something with the response */

    /* call r.Close() explicitly? */
}

The code works fine today, but the connections to the server stay open for quite some time. (checked using TCPView)

Is there a benefit to calling the Close() method explicitly? Is it recommended, or maybe recommended not to do it, and why?

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
BlackTigerX
  • 6,006
  • 7
  • 38
  • 48

4 Answers4

30

When Dispose() is called on WebResponse (HttpWebReponse's base class), it calls it's Close() method for you. A quick glance using Reflector confirms this.

Edit (in response to comment): If it's called for you already, why call it explicitly? For the sake of clarity? I think if people understand the using (X x = ...) statement, they'll understand that it is closing the underlying connection. You gain nothing by calling it explicitly in this case.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
18

The using keyword is a syntactic sugar for try/finally block, which wraps around your HttpWebResponse, since it implements IDisposable. When in the finally clause, it will call the Dispose() method, which will call Close(). This means that you don't have to explicitly call the Close() method.

Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
2

I believe Close is the method that implements IDisposable.Dispose, therefore there is definitely no need to call the Close method beforehand. It is fully redundant.

Noldorin
  • 144,213
  • 56
  • 264
  • 302
  • A method called "Close" cannot implement an interface member called "Dispose". One can call the other, but they are distinct methods. – harpo Sep 02 '13 at 10:04
  • Not quite true. In VB.NET, this is actually possible. It's only in C# that it is (technically) not, though one can imitate it very easily. – Noldorin Sep 02 '13 at 13:38
  • 1
    I stand corrected. http://stackoverflow.com/questions/5533659/in-c-is-it-possible-to-implement-an-interface-member-using-a-member-with-a-dif – harpo Sep 02 '13 at 13:45
0

If you have significant code inside the using after you have finished consuming the response then sure calling close is ok. However you might want to consider refactoring your code so that code that doesn't need the response isn't inside the using block.

That said closeing the response doesn't necessarily close the connection. The HTTP/1.1 protocol makes provision for the connection to remain open to make subsequent requests quicker.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • 1
    FWIW, The first sentence of this answer needs to be read carefully. It would be clearer to start out by explaining, as other answers do, that `using` does `close` for you - you don't need to call it yourself. Once you are clear about that, then you can understand what the sentence is saying: if for some reason you want to `close` (to release resources) mid-way through the `using` block, then go ahead and do so - but a cleaner design would be to *only keep inside the using block, code that uses the result*. – ToolmakerSteve Jan 16 '17 at 17:12