{"id":3821,"date":"2015-05-27T13:00:59","date_gmt":"2015-05-27T13:00:59","guid":{"rendered":"http:\/\/writeasync.net\/?p=3821"},"modified":"2015-05-27T02:25:21","modified_gmt":"2015-05-27T02:25:21","slug":"async-in-the-wild-wcf","status":"publish","type":"post","link":"http:\/\/writeasync.net\/?p=3821","title":{"rendered":"Async in the wild: WCF"},"content":{"rendered":"<p><a href=\"http:\/\/www.dotnetfoundation.org\/blog\/wcf-is-open-source\">WCF (client) is open source.<\/a> An interesting side effect of this development is the opportunity for async enthusiasts to pore over some production-ready async code. Since <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms735119(v=vs.85).aspx\">WCF was released even before .NET 3.5<\/a>, it has several clever patterns for sensibly dealing with the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms228963(v=vs.85).aspx\">legacy async programming model<\/a> (IAsyncResult).<\/p>\n<p>For example, let&#8217;s take a look at <a href=\"https:\/\/github.com\/dotnet\/wcf\/blob\/master\/src\/System.Private.ServiceModel\/src\/System\/ServiceModel\/Channels\/ChainedAsyncResult.cs\"><code>ChainedAsyncResult<\/code><\/a>. This is an extension on top of the base <a href=\"https:\/\/github.com\/dotnet\/wcf\/blob\/master\/src\/System.Private.ServiceModel\/src\/Internals\/System\/Runtime\/AsyncResult.cs\"><code>AsyncResult<\/code><\/a> (itself a more or less textbook example of APM with a few WCF-isms like fatal exception handling). As the name suggests, it chains two successive asynchronous operations together in a simple and repeatable way. An example use case is contained within <a href=\"https:\/\/github.com\/dotnet\/wcf\/blob\/master\/src\/System.Private.ServiceModel\/src\/System\/ServiceModel\/Channels\/ServiceChannelFactory.cs\"><code>ServiceChannelFactory+TypedServiceChannelFactory<\/code><\/a>:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprotected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)\r\n{\r\n    return new ChainedAsyncResult(timeout, callback, state, base.OnBeginClose, base.OnEndClose,\r\n    _innerChannelFactory.BeginClose, _innerChannelFactory.EndClose);\r\n}\r\n<\/pre>\n<p>The close operation on the typed channel factory consists of closing the base factory followed by the inner factory. Without the luxury of <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.threading.tasks.task.continuewith(v=vs.110).aspx\">ContinueWith<\/a>, this could be a relatively complex and error-prone operation. But ChainedAsyncResult encapsulates the complexity and takes care of the many &#8220;gotchas&#8221; including proper synchronous completion handling (i.e. exit the callback and let the original caller handle completion to avoid <a href=\"http:\/\/blogs.msdn.com\/b\/mjm\/archive\/2005\/05\/04\/414793.aspx\">stack dives<\/a>) and timeout behavior (using the oft-employed <a href=\"https:\/\/github.com\/dotnet\/wcf\/blob\/master\/src\/System.Private.ServiceModel\/src\/Internals\/System\/Runtime\/TimeoutHelper.cs\"><code>TimeoutHelper<\/code><\/a>).<\/p>\n<p>The <a href=\"https:\/\/github.com\/dotnet\/wcf\">open source portion of WCF<\/a> is relatively thin thus far. Nonetheless, there are many interesting async gems already there for the curious. Take a look!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WCF (client) is open source. An interesting side effect of this development is the opportunity for async enthusiasts to pore over some production-ready async code. Since WCF was released even before .NET 3.5, it has several clever patterns for sensibly&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,91],"tags":[],"class_list":["post-3821","post","type-post","status-publish","format-standard","hentry","category-async","category-design"],"_links":{"self":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/3821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3821"}],"version-history":[{"count":0,"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/3821\/revisions"}],"wp:attachment":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3821"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}