title: How do I wrap a Web Api's async call and results as a synchronous method?
body: <p>I need to integrate a third party's Web API methods into a WCF service.
  The WCF Service will be called by another external party, and they expect the call and return to be synchronous.</p>
  
  <p>I am able to get results from the API with the usual <code>RestClient.ExecuteAsync</code>.</p>
  
  <p>I put together the following for synchronous calls:</p>
  
  <pre><code>public static List&lt;Books&gt; GetSyncBooks(int companyId)
  {
      var response = GetSynchronousBooks(companyId);  
      var content = response.Result.Content;
      List&lt;Books&gt; result = new List&lt;Books&gt;();
  
      return Helpers.JSONSerialiser.Deserialize&lt;BookList&gt;(content);
  }
  
  
  async private static Task&lt;IRestResponse&gt; GetSynchronousBooks(int companyId)
  {
      var request = BuildGETRequest("Book", companyId);
      var response = await RestSharpHelper.ExecuteSynchronousRequest(request);
  
      return response;
  }
  
  public static Task&lt;IRestResponse&gt; ExecuteSynchronousRequest(RestRequest request)
  {
      var client = new RestClient(BaseUrl);
      client.AddHandler("application/json", new RestSharpJsonDotNetDeserializers());
  
      var tcs = new TaskCompletionSource&lt;IRestResponse&gt;(TaskCreationOptions.AttachedToParent);
  
      client.ExecuteAsync(request, (restResponse, asyncHandle) =&gt;
      {                
          if (restResponse.ResponseStatus == ResponseStatus.Error)
              tcs.SetException(restResponse.ErrorException);
          else
              tcs.SetResult(restResponse);
      });
      return tcs.Task;
  // BREAKPOINT here shows TASK value as
  //  Id = 1, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"
  }
  </code></pre>
  
  <p>The problem, however, is that I never get a result using this. The response content is always null. What am I doing wrong?</p>
  
  <p>EDIT: Thanks Stephen. I have seen your name on some of the questions here on this subject: your score seems to indicate you know your way around this. I have indeed implemented the wcf service calls as you indicated based on your answer at another question. Can I ask you a related follow-up question? How scalable are async WCF service calls like this example? Would it "just work" for multiple simultaneous calls in the range of 10 to 100 per second, ignoring the processing overhead downstream? </p>