I assume that your WCF service is hosted in ASP.NET.
Your problem is here: response.Result
. I explain this deadlock situation on my blog. In summary, await
will capture the current "context" (in this case, an ASP.NET request context) and will use that to resume the async
method. However, the ASP.NET request context only allows one thread at a time, so if the request thread is blocked (calling response.Result
), then the async
method can never continue and you get a deadlock.
The solution is to correct this misunderstanding:
The WCF Service will be called by another external party, and they expect the call and return to be synchronous.
Since you're dealing with a client/server scenario, you don't have to make it synchronous. The asynchrony of the client is completely independent from the asynchrony of the server.
So, just implement your WCF service asynchronously:
public static Task<List<Books>> GetBooksAsync(int companyId)
{
var response = await GetBooksAsync(companyId);
var content = response.Content;
List<Books> result = new List<Books>();
return Helpers.JSONSerialiser.Deserialize<BookList>(content);
}
The client can still call it synchronously if they wish to.