Do using statements and await keywords play nicely in c#

swingdoctor picture swingdoctor · May 15, 2013 · Viewed 17.8k times · Source

I have a situation where I am making an async call to a method that returns and IDisposable instance. For example:

HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com"));

Now before async was on the scene, when working with an IDisposable instance, this call and code that used the "response" variable would be wrapped in a using statement.

My question is whether that is still the correct approach when the async keyword is thrown in the mix? Even though the code compiles, will the using statement still work as expected in both the examples below?

Example 1

using(HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com")))
{
    // Do something with the response

    return true;
}

Example 2

using(HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com")))
{
    await this.responseLogger.LogResponseAsync(response);

    return true;
}

Answer

Jon Skeet picture Jon Skeet · May 15, 2013

Yes, that should be fine.

In the first case, you're really saying:

  • Asynchronously wait until we can get the response
  • Use it and dispose of it immediately

In the second case, you're saying:

  • Asynchronously wait until we can get the response
  • Asynchronously wait until we've logged the response
  • Dispose of the response

A using statement in an async method is "odd" in that the Dispose call may execute in a different thread to the one which acquired the resource (depending on synchronization context etc) but it will still happen... assuming the thing you're waiting for ever shows up or fail, of course. (Just like you won't end up calling Dispose in non-async code if your using statement contains a call to a method which never returns.)