Freigeben über


Implementing the Asynchronous Programming Model

After the fun I had yesterday building my Yahoo Services Search Proxy (see my previous post) today I decided to see if I could extend my proxy to support the Asynchronous Programming Model.

And what is more, I wanted to use the TDD process to do it.  That meant that I would have to stub out implementations of things like HttpWebRequest/Response etc.  It took all afternoon and when I got done I had created my own implementation of the APM both as a provider and consumer (a really great learning experience) but in the end I must confess that I began to wonder if it makes sense to add this to the proxy class.

Monday and Tuesday I was in Jeff Richter's most excellent Advanced Threading class where we covered many of the reasons why the APM is a beautiful thing.  To be honest with you I only thought I understood threading before.  Now I know there is so much more that I don't truly understand about it.  However, one thing I know for sure.  If you want to build highly scalable and robust apps you should be asynchronous as much as possible.  There are two styles of async for .NET.  One is the APM which is interface and callback based and the other is the Event Based programming model (EPM) which is designed for client side developers.

Jeff told us that he is not a fan of EPM but to be honest with you I think that many developers will never do APM (even though it isn't that hard to do).  EPM is much easier but as Jeff points out probably not suitable outside of a client side project.

The more I think about it... WCF implements the APM in generated proxies (with the /async switch on SvcUtil).  If they can do it... I can do it.  I have the guts of it working...  Now I just need to think through the design a little more.  My class will then implement both APM and EPM so you can take your pick.

Fun to be coding again... so much to learn.

Comments

  • Anonymous
    March 07, 2008
    PingBack from http://msdnrss.thecoderblogs.com/2008/03/07/implementing-the-asynchronous-programming-model-2/

  • Anonymous
    March 08, 2008
    The WCF implementation from Microsoft has a very nice implementation of the APM. It wraps async operations in commands and derives these commands from a baseclass. The baseclass does all the dirty work - threading (as in sophisticated threading) - and also manages timeouts, as WCF Begin/End method pairs are usually parameterized with a timeout in WCF. And since you are now the Evangelist for WCF and WF, maybe you can urge the product group to make some of the classes (System.ServiceModel.Channels.InputQueue<T>, System.ServiceModel.AsyncResult) publicly available for reuse. That would make implementing channels for WCF much  easier:-) What I don't like about WCF: I think it still "fosters" the use of the synchronuous request-reply model. I know it supports async invocation, OneWay, and (the truly async) duplex models, but most of the people will issue blocking calls against the proxy. If you use the ChannelFactory<IMyService> programming model, you don't even get the async Begin/End on the client for free - though you only need to pay a small fee. The nicest asynchronuous programming model in a microsoft product is definitely sql server service broker, which is beautiful.

  • Anonymous
    March 09, 2008
    Good points - I should do a blog post on how do call WCF services async.  I will definitely urge the product team on.  If you could tell them what you wanted, what is your wish list?  I'd invite you and anyone else who wants to blog their WCF wish list and send me a note to let me know about it.  ron (dot) jacobs (at) microsoft (dot) com