title: Calling async methods from non-async code
body: <p>I'm in the process of updating a library that has an API surface that was built in .NET 3.5. As a result, all methods are synchronous. I can't change the API (i.e., convert return values to Task) because that would require that all callers change. So I'm left with how to best call async methods in a synchronous way. This is in the context of ASP.NET 4, ASP.NET Core, and .NET/.NET Core console applications.</p>
  
  <p>I may not have been clear enough - the situation is that I have existing code that is not async aware, and I want to use new libraries such as System.Net.Http and the AWS SDK that support only async methods. So I need to bridge the gap, and be able to have code that can be called synchronously but then can call async methods elsewhere.</p>
  
  <p>I've done a lot of reading, and there are a number of times this has been asked and answered.</p>
  
  <p><a href="https://stackoverflow.com/questions/38865307/calling-async-method-from-non-async-method">Calling async method from non async method</a></p>
  
  <p><a href="https://stackoverflow.com/questions/14485115/synchronously-waiting-for-an-async-operation-and-why-does-wait-freeze-the-pro">Synchronously waiting for an async operation, and why does Wait() freeze the program here</a></p>
  
  <p><a href="https://stackoverflow.com/questions/19010337/calling-an-async-method-from-a-synchronous-method">Calling an async method from a synchronous method</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5095183/how-would-i-run-an-async-taskt-method-synchronously">How would I run an async Task&lt;T&gt; method synchronously?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/22628087/calling-async-method-synchronously">Calling async method synchronously</a></p>
  
  <p><a href="https://stackoverflow.com/questions/9343594/how-to-call-asynchronous-method-from-synchronous-method-in-c">How to call asynchronous method from synchronous method in C#?</a></p>
  
  <p>The problem is that most of the answers are different! The most common approach I've seen is use .Result, but this can deadlock. I've tried all the following, and they work, but I'm not sure which is the best approach to avoid deadlocks, have good performance, and plays nicely with the runtime (in terms of honoring task schedulers, task creation options, etc). Is there a definitive answer? What is the best approach?</p>
  
  <pre><code>private static T taskSyncRunner&lt;T&gt;(Func&lt;Task&lt;T&gt;&gt; task)
      {
          T result;
          // approach 1
          result = Task.Run(async () =&gt; await task()).ConfigureAwait(false).GetAwaiter().GetResult();
  
          // approach 2
          result = Task.Run(task).ConfigureAwait(false).GetAwaiter().GetResult();
  
          // approach 3
          result = task().ConfigureAwait(false).GetAwaiter().GetResult();
  
          // approach 4
          result = Task.Run(task).Result;
  
          // approach 5
          result = Task.Run(task).GetAwaiter().GetResult();
  
  
          // approach 6
          var t = task();
          t.RunSynchronously();
          result = t.Result;
  
          // approach 7
          var t1 = task();
          Task.WaitAll(t1);
          result = t1.Result;
  
          // approach 8?
  
          return result;
      }
  </code></pre>