The "async" methods mentioned in the documentation do not return Task so they can't be used with async/await
as they are. They actually require callbacks to call on success or failure.
E.g. the signature for GetAsync
is :
public virtual void GetAsync<TResponse>(string relativeOrAbsoluteUrl,
Action<TResponse> onSuccess,
Action<TResponse, Exception> onError)
This is the APM-style of asynchronous functions and can be converted to Task-based functions using a TaskCompletionSource, eg:
public static Task<TResponse> GetTask<TResponse>(this JsonServiceClient client, string url)
{
var tcs = new TaskCompletionSource<TResponse>();
client.GetAsync<TResponse>(url,
response=>tcs.SetResult(response),
(response,exc)=>tcs.SetException(exc)
);
return tcs.Task;
}
You can call the extension method like this:
var result = await client.GetTask<SomeClass>("someurl");
Unfortunatelly, I had to name the method GetTask for obvious reasons, even though the convention is to append Async
to methods that return Task
.