2

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.

Community
  • 1
  • 1
Serberuss
  • 2,247
  • 4
  • 22
  • 40
  • 2
    Can't you have the web-service reference generate TAP methods for you? – usr May 22 '14 at 11:20
  • 2
    Have you tried to add a breakpoint to `TransferCompletion` and see whether it actually get called whether it does what it should? Especially the `e.UserState == source` check looks suspicious to me, since you don't seem to be setting any user state anywhere. – svick May 22 '14 at 11:22
  • TransferCompletion never gets called. I don't have the option to generate TAP methods either – Serberuss May 22 '14 at 12:54
  • Does the synchronous version return? Maybe the remote side never responds or takes a long time. (This is a vague guess but I see nothing else that is suspicious). Next advice would be to create a standalone 10 line repro console application to make sure there is nothing in your environment causing problems. – usr May 22 '14 at 12:59
  • Synchronous version definitely runs and if I just use the old event handler way that works fine as well it seems to be this approach isn't working – Serberuss May 22 '14 at 13:02
  • Do you have any call `Result` or `Wait()` on any task on your application? – Paulo Morgado May 22 '14 at 14:34
  • No I don't. I took usr's advice and created a new project this time using the W3Schools web service (http://www.w3schools.com/webservices/tempconvert.asmx) and I'm getting the same issue. I must be doing something wrong here – Serberuss May 22 '14 at 14:39
  • Are you awaiting in UI thread (SynchronizationContext.Current != null?) – SalientBrain May 27 '14 at 06:34

0 Answers0