The code below causes a code analysis warning CA2000, claiming that I need to dispose of getRequest
along all code paths before it goes out of scope.
Tweaking the code slightly, for example removing the when
from the catch
, eliminates the warning.
try
{
using (var getRequest = new HttpRequestMessage(HttpMethod.Get, uri))
{
using (var result = client.SendAsync(getRequest).Result)
{
}
}
}
catch (AggregateException exc) when (exc.InnerExceptions.Count >= 0)
{
}
On which code path is dispose not called, and why does catch/when affect things?
Note: I'm using VS2015's built in code analysis. CA2000 is in the managed binary analysis ruleset, and is (for me) unchecked by default for new solutions. (VS2017 also emits the same warnings)