I have a web reference which uses the Event-based Asynchronous Pattern. I am trying to convert this to use the Task based model so that I can use await/async. However I am having issues with this as it does not seem to do the call. I am using MonoTouch C# for this project.
Earlier I found this Stack overflow post which seemed to detail what I'm trying to do: How can I use async/await to call a webservice?
I managed to setup these methods and this is what my code looks like so far:
Authentication
var client = new AgentService();
GetUserDetailsCompletedEventArgs response = await client.GetUserDetailsAsyncTask(username, password);
if (response.Result != null)
// Do stuff
Agent Service Extensions
public static Task<GetUserDetailsCompletedEventArgs> GetUserDetailsAsyncTask(this AgentService client, string username, string password)
{
var source = new TaskCompletionSource<GetUserDetailsCompletedEventArgs>(null);
client.GetUserDetailsCompleted += (sender, e) => TransferCompletion(source, e, () => e);
client.GetUserDetailsAsync(username, password);
return source.Task;
}
private static void TransferCompletion(TaskCompletionSource<GetUserDetailsCompletedEventArgs> source, AsyncCompletedEventArgs e, Func<GetUserDetailsCompletedEventArgs> getResult)
{
if (e.UserState == source)
if (e.Cancelled)
source.TrySetCanceled();
else if (e.Error != null)
source.TrySetException(e.Error);
else
source.TrySetResult(getResult());
}
At the moment the GetUserDetailsAsyncTask method is called and the event handler is set but is never reached. After the await line is called the statement underneath it is never reached either and just skips to the end of the method. No error message is displayed and no exception is thrown. I'm not sure where I'm going wrong and I'd appreciate any guidance to help me solve this.