Tuesday, August 12, 2014

From AsyncCompleted to async await

Since we got the magic of async/await on C# things got much easier on the developer front. Working with Tasks is the key to it, but we also have two older approaches in .NET framework that we need to deal with.
  1.  APM (Asynchronous Programming Model) The pair of Being/End methods.
  2.  EAP (Event-based Asynchronous Programming) The MethodAsync/MethodCompleted pair.
Converting from APM to Tasks is achieved by means of the  System.Threading.Tasks.TaskFactory  class and its generic counterpart.

Now converting from EAP to Tasks  requires us to use the TaskCompletionSource<T> class and notify of the state of the task as it goes. See the example of a "handshake" operation in a WCF service. I am also using the approach from my older post regarding events and anonymous methods. The return type of the WCF operation is WrappedResultOfString, and that will be the actual generic parameter of the Task<T> that the HandshakeAsync method will produce.

 public Task<WrappedResultofString> HandshakeAsync(Credentials credentials)
        {
            var tcs = new TaskCompletionSource<WrappedResultofString>();
            var client = _serviceFactory.Invoke(); //get a WCF client instance
            EventHandler<HandshakeCompletedEventArgs> callback = null;
            callback = (s, e) =>
            {
                client.HandshakeCompleted -= callback;
                 if (e.Error != null)
                    tcs.TrySetResult(new WrappedResultOfstring
                    {
                        Value = null,
                        IsFailed = true,
                        Error =  e.Error
                    });
                 else if (e.Cancelled)
                    tcs.TrySetResult(new WrappedResultOfstring
                    {
                        Value = null,
                        IsFailed = true,
                        Error = "Operation Cancelled"
                    });
                    tcs.TrySetResult(e.Result);

            };
            client.HandshakeCompleted += callback;
            client.HandshakeAsync(credentials.UserName, credentials.Password);
            return tcs.Task;
        }

//then this is ready to be used with async/await

public async void LoginMethod()
{
   var credentials = //build your credentials;
  //now we can use 'await'
   var handshake = await this.HandshakeAsync(credentials);
}


This is particularly useful when dealing with WCF proxies in silverlight or windows phone apps where we only get EAP-based operations.