Do using statements and await keywords play nicely in c#

北城以北 提交于 2019-11-26 10:33:20

问题


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;
}

回答1:


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.)



来源:https://stackoverflow.com/questions/16566547/do-using-statements-and-await-keywords-play-nicely-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!