{"id":201,"date":"2009-05-18T10:18:28","date_gmt":"2009-05-18T08:18:28","guid":{"rendered":"http:\/\/dev.flauschig.ch\/wordpress\/?p=201"},"modified":"2009-05-18T11:10:54","modified_gmt":"2009-05-18T09:10:54","slug":"asynchroneous-operations-in-wcf","status":"publish","type":"post","link":"http:\/\/dev.flauschig.ch\/wordpress\/?p=201","title":{"rendered":"Asynchronous Operations in WCF"},"content":{"rendered":"<p>There are multiple possibilities to call an Operation Asynchronous from a WCF Service:<\/p>\n<h4><span style=\"color: #000000;\">Event Based Model<\/span><\/h4>\n<p>In this model, you register to a <code>Completed<\/code>-Event and call your Operation with an <code>Async<\/code> appended. The Callback Method is run on the same Thread that was used to call the Async Request, so you don&#8217;t need to <code>invoke<\/code> it in case you&#8217;re modifying the UI.<\/p>\n<p>Example:<\/p>\n<pre class=\"brush: csharp; light: true; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npublic void AsyncCall()\r\n{\r\n    MyServiceClient client = new MyServiceClient();\r\n    client.SomeMethodCompleted += SomeMethodCompletedCallback;\r\n    client.SomeMethodAsync(&lt;Your Parameters&gt;);\r\n}\r\n\r\npublic void SomeMethodCompletedCallback(object sender, SomeMethodCompletedEventArgs e)\r\n{\r\n    MyServiceClient client = sender as MyServiceClient;\r\n    if (client != null)\r\n    {\r\n        client.Close();\r\n    }\r\n    &lt;Your Return Value&gt; = e.Result;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><span style=\"color: #000000;\">IAsyncResult Model (Client-Side)<\/span><\/h4>\n<p>In this model, you call your Operation with a prepending <code>Begin<\/code>. Note that the Callback is running on a Worker-Thread so if you&#8217;re modifying the UI, don&#8217;t forget to <code>invoke<\/code> in the Callback.<\/p>\n<p>Example:<\/p>\n<pre class=\"brush: csharp; light: true; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npublic void AsyncCall()\r\n{\r\n    MyServiceClient client = new MyServiceClient();\r\n    client.BeginSomeMethod(&lt;Your Parameters&gt;, new AsyncCallback(OnEndSomeMethod), client);\r\n}\r\n\r\npublic void OnEndSomeMethod(IAsyncResult asyncResult)\r\n{\r\n    MyServiceClient client = asyncResult.AsyncState as MyServiceClient;\r\n    if (client != null)\r\n    {\r\n        &lt;Your Return Value&gt; = client.EndSomeMethod(asyncResult)\r\n        client.Close();\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><span style=\"color: #000000;\">IAsyncResult Model (Server-Side)<\/span><\/h4>\n<p>This is the only &#8220;true&#8221; Asynchronous Model. It will run in a Server-Thread so you are able to get the status of a pending Operation or even cancel it. The implementation is more time-consuming than the other two but, in some cases, worth the effort.<br \/>\nI suggest you Google for some examples.<br \/>\n&nbsp;<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<p>There is a very good series of posts at <a href=\"http:\/\/www.danrigsby.com\">Dan Rigsby&#8217;s Blog<\/a><br \/>\nstarting at <a href=\"http:\/\/www.danrigsby.com\/blog\/index.php\/2008\/03\/18\/async-operations-in-wcf-event-based-model\/\">this Post<\/a>. Have a look at it to get more information and examples.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are multiple possibilities to call an Operation Asynchronous from a WCF Service: Event Based Model In this model, you register to a Completed-Event and call your Operation with an Async appended. The Callback Method is run on the same Thread that was used to call the Async Request, so you don&#8217;t need to invoke &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4,3],"tags":[65,66,16],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-roemer","4":"post-201","6":"format-standard","7":"category-csharp","8":"category-programming","9":"post_tag-async","10":"post_tag-begin","11":"post_tag-wcf"},"acf":[],"views":4347,"_links":{"self":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/201"}],"collection":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=201"}],"version-history":[{"count":0,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/201\/revisions"}],"wp:attachment":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=201"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}