1

I have created my own way to essentially set ConfigureAwait(false) just once at the top of each async method which allows me to not have to append ConfigureAwait(false) at the end of each await call. Now I can just do something like:

  public async Task MyMethod()
  {
     await ConfigureAwait.Off;

     var data = await _client.GetDataAsync();
  }

Here's the problem: I have a very large solution and I'm now trying to find an efficient way to add this to any async method and remove any redundant .ConfigureAwait(false) calls. I've tried Resharper's search for pattern without any luck at all. Any ideas or suggestions are greatly appreciated.

This below doesn't work good at all using resharpers Search for Pattern:

Search Pattern:

 public async Task $method$($args1$) { $stmt$ }

Replace Pattern:

 public async Task $method$($args1$) 
 {
     await ConfigureAwait.Off;

     $stmt$
 }

Note: This also doesn't account for async methods that may already have it defined already as well as removing any redundant .ConfigureAwait(false);

TMan
  • 4,044
  • 18
  • 63
  • 117
  • What is `await ConfigureAwait.Off` i feel out of the loop here, and its the first time i have ever seen it – TheGeneral Jan 14 '19 at 03:01
  • It's just a very simple class with a static getter only property that will temporarily remove the SynchronizationContext. I can send you entire code directly if you want. It's nothing magical: [DebuggerStepThrough] public class ConfigureAwait : INotifyCompletion { private ConfigureAwait() { } public static ConfigureAwait Off { get; } = new ConfigureAwait(); – TMan Jan 14 '19 at 03:12
  • What are you trying to achieve by jumping off the current sync context like this? In general, it isn't a good idea, even if you're trying to re-mediate deadlocks. See https://stackoverflow.com/q/15363413/1768303 – noseratio Jan 14 '19 at 03:38
  • @noseratio That's appears to be different than what my implementation is doing. In the link, he seems to be trying to actually switch back to the UI thread intentionally where all my implementation is doing is essentially setting the SynchronizationContext to null OnCompleted before continuation. – TMan Jan 14 '19 at 19:50
  • @TMan, I think they in general decided against using a construct like `await switchContext`, regardless of the target context (or lack of it). They recommend simply using `Task.Run` to run the desired code on a pool thread without context. Would that be to expensive in your case? – noseratio Jan 14 '19 at 20:56

0 Answers0