{"id":258,"date":"2008-04-29T21:07:19","date_gmt":"2008-04-30T04:07:19","guid":{"rendered":"http:\/\/kennyw.com\/indigo\/258"},"modified":"2008-04-29T21:07:19","modified_gmt":"2008-04-30T04:07:19","slug":"when-to-use-async-service-operations","status":"publish","type":"post","link":"https:\/\/kennyw.com\/?p=258","title":{"rendered":"When to use Async Service Operations"},"content":{"rendered":"<p>I was recently asked about the motivation for choosing asynchronous service operations in WCF (i.e. [OperationContract(<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.servicemodel.operationcontractattribute.asyncpattern.aspx\">AsyncPattern<\/a> = true)]). <\/p>\n<p>If you have an operation that is blocking (accessing SQL, Channels, etc) then you should use AsyncPattern=true.&#160; That way you&#8217;ll free up whatever thread we&#8217;re using to call your operation from. The general idea is that if you have a blocking call then you should use the async version and it should transparently play well with us. <\/p>\n<p>Put another way: if you are calling a method that returns an AsyncResult (i.e. you&#8217;re accessing SQL, or using sockets, files, or channels), then you can wrap that IAsyncResult and return it from the BeginXXX call (or return the raw asyncresult depending on your scenario). <\/p>\n<p>If you aren&#8217;t doing something that&#8217;s &quot;natively async&quot;, then you shouldn&#8217;t be using AsyncPattern=true. That is, you shouldn&#8217;t just create a thread just for the sake of performing &quot;background work&quot; as part of an asynchronous operation. Note that it is legitimate to spawn a thread because your operation happens to kick off work that is outside of its completion scope (though in that case you should just have a synchronous method, not an async one). <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was recently asked about the motivation for choosing asynchronous service operations in WCF (i.e. [OperationContract(AsyncPattern = true)]). If you have an operation that is blocking (accessing SQL, Channels, etc) then you should use AsyncPattern=true.&#160; That way you&#8217;ll free up whatever thread we&#8217;re using to call your operation from. The general idea is that if [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-258","post","type-post","status-publish","format-standard","hentry","category-indigo"],"_links":{"self":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/posts\/258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=258"}],"version-history":[{"count":0,"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/posts\/258\/revisions"}],"wp:attachment":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}