{"id":604,"date":"2016-02-12T17:45:14","date_gmt":"2016-02-12T17:45:14","guid":{"rendered":"http:\/\/tpodolak.com\/blog\/?p=604"},"modified":"2016-02-22T23:44:13","modified_gmt":"2016-02-22T23:44:13","slug":"using-asyncawait-iasyncresult-pattern","status":"publish","type":"post","link":"https:\/\/tpodolak.com\/blog\/2016\/02\/12\/using-asyncawait-iasyncresult-pattern\/","title":{"rendered":"Using async\/await with IAsyncResult pattern"},"content":{"rendered":"<p>Even though in my current project we use .NET 4.5 we still have lots of references to internal libraries targeting .NET 4.0. Usually it is not a problem, however some of these libraries follow  <a href=\"https:\/\/msdn.microsoft.com\/pl-pl\/library\/jj152938(v=vs.110).aspx\"> Asynchronous Programming Model<\/a> pattern. This approach makes the code quite ugly because we end up with lots of nested callbacks.<\/p>\n<pre lang=\"csharp\">\r\n\/\/ rest of the code omitted for brevity\r\nprivate static void GetHostEntryIAsyncResultPattern()\r\n{\r\n    DnsService dnsService = new DnsService();\r\n    dnsService.BeginGetHostEntry(GoogleHost, googleAsyncResult =>\r\n    {\r\n        var googleHostResult = dnsService.EndGetHostEntry(googleAsyncResult);\r\n        Console.WriteLine($\"Result from IAsyncResultPattern: {GoogleHost} - {FormatHostEntry(googleHostResult)}\");\r\n        dnsService.BeginGetHostEntry(YahooHost, yahooAsyncResult =>\r\n        {\r\n            var yahooHostResult = dnsService.EndGetHostEntry(yahooAsyncResult);\r\n            Console.WriteLine($\"Result from IAsyncResultPattern: {YahooHost} - {FormatHostEntry(yahooHostResult)}\");\r\n        }, null);\r\n    }, null);\r\n}\r\n\/\/ rest of the code omitted for brevity\r\n<\/pre>\n<p>Fortunately thanks to <\/p>\n<pre lang=\"csharp\">\r\nTask.Factory.FromAsync\r\n<\/pre>\n<p>method it is possible to quite easily make Begin\/End methods work with async\/await pattern. All you have to do is to pass Begin\/End methods as an arguments to <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.threading.tasks.taskfactory.fromasync(v=vs.110).aspx\">Task.Factory.FromAsync<\/a> and you are good to go. The original code after refactoring looks like that<\/p>\n<pre lang=\"csharp\">\r\n\/\/ rest of the code omitted for brevity\r\nvar dnsService = new DnsService();\r\nvar googleHostResult = await Task.Factory.FromAsync((callback, stateObject) => dnsService.BeginGetHostEntry(GoogleHost, callback, stateObject), dnsService.EndGetHostEntry, null);\r\nConsole.WriteLine($\"Result from async\/await: {GoogleHost} - {FormatHostEntry(googleHostResult)}\");\r\n\r\nvar yahooHostResult = await Task.Factory.FromAsync((callback, stateObject) => dnsService.BeginGetHostEntry(YahooHost, callback, stateObject), dnsService.EndGetHostEntry, null);\r\nConsole.WriteLine($\"Result from async\/await: {YahooHost} - {FormatHostEntry(yahooHostResult)}\");\r\n\/\/ rest of the code omitted for brevity\r\n<\/pre>\n<p>I think everyone will agree with me that refactored version is much better than original one.<br \/>\nSource code for this post can be found <a href=\"https:\/\/github.com\/tpodolak\/Blog\/tree\/master\/BeginInvokeEndInvokeAsyncAwait\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Even though in my current project we use .NET 4.5 we still have lots of references to internal libraries targeting .NET 4.0. Usually it is not a problem, however some of these libraries follow Asynchronous Programming Model pattern. This approach makes the code quite ugly because we end up with lots of nested callbacks. \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[287,288],"tags":[289,290],"class_list":["post-604","post","type-post","status-publish","format-standard","hentry","category-async-await","category-iasyncresult","tag-async-await","tag-iasyncresult"],"_links":{"self":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/604","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/comments?post=604"}],"version-history":[{"count":10,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/604\/revisions"}],"predecessor-version":[{"id":617,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/604\/revisions\/617"}],"wp:attachment":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/media?parent=604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/categories?post=604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/tags?post=604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}