title: Set SynchronizationContext to null instead of using ConfigureAwait(false)
body: <p>I have a library that exposes synchronous and asynchronous versions of a method, but under the hood, they both have to call an async method. I can't control that async method (it uses async/await and does <em>not</em> use <code>ConfigureAwait(false)</code>), nor can I replace it.</p>
  
  <p>The code executes in the context of an ASP .NET request, so to avoid deadlocks, here's what I've done:</p>
  
  <pre><code>var capturedContext = SynchronizationContext.Current;
  try
  {
      // Wipe the sync context, so that the bad library code won't find it
      // That way, we avoid the deadlock
      SynchronizationContext.SetSynchronizationContext(null);
  
      // Call the async method and wait for the result
      var task = MyMethodAsync();
      task.Wait();
  
      // Return the result
      return task.Result;
  }
  finally
  {
      // Restore the sync context
      SynchronizationContext.SetSynchronizationContext(capturedContext);
  }
  </code></pre>
  
  <p>Does this produce the same effect as if MyMethodAsync had used <code>ConfigureAwait(false)</code> on all of its await's? Are there some other problems with this approach that I'm overlooking?</p>
  
  <p>(MyMethodAsync is completely unaware that it's being run in an ASP .NET context, it doesn't do any calls to <code>HttpContext.Current</code> or anything like that. It  just does some async SQL calls, and the writer didn't put <code>ConfigureAwait(false)</code> on any of them)</p>