I need to write an NDepend rule that checks if every await of an Async Method uses ConfigureAwait(false).
I check if a method uses ConfigureAwait(Boolean). If that method doesn't use it, it has violated the rule.
from m in JustMyCode.Methods
where
!(m.IsUsingMethod("System.Threading.Tasks.Task.ConfigureAwait(Boolean)") ||
m.IsUsingMethod("System.Threading.Tasks.Task<TResult>.ConfigureAwait(Boolean)")) &&
m.IsAsync
select new {
m,
m.MethodsCalled
}
This code works fine if you are sure that every async method uses one await. But what if a method uses two awaits? then you could count all occurrences of await in that method.
Example
public async Task Foo()
{
await DoSomething();
}
public async Task Bar()
{
await DoSomething().ConfigureAwait(false);
}
public async Task Stone()
{
await DoSomething();
await DoSomething().ConfigureAwait(false);
}
Foo and Stone Has violated the rule.
With my code I can see that Foo has violated the rule, but Stone hasn't violated It.
With 'MethodsCalled' I am able to check if a method uses ConfigureAwait(), which is nice. But again I am not able to check how many occurrences of await and ConfigureAwait() 'Stone' has.