{"id":1504,"date":"2019-09-21T23:59:53","date_gmt":"2019-09-21T13:59:53","guid":{"rendered":"https:\/\/ntsblog.homedev.com.au\/?p=1504"},"modified":"2019-09-22T00:00:01","modified_gmt":"2019-09-21T14:00:01","slug":"c-extension-awaitresult","status":"publish","type":"post","link":"https:\/\/ntsblog.homedev.com.au\/index.php\/2019\/09\/21\/c-extension-awaitresult\/","title":{"rendered":"c# extension &#8211; AwaitResult"},"content":{"rendered":"<div id=\"ntsbl-594443247\" class=\"ntsbl-before-content ntsbl-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6288941070289539\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:inline-block;width:728px;height:90px;\" \ndata-ad-client=\"ca-pub-6288941070289539\" \ndata-ad-slot=\"9356781486\"><\/ins> \n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div><h2>The Problem<\/h2>\n<p>When using async coding there are various places where you cannot use the await operator, e.g. static main in a console app.<\/p>\n<p>You need to add code to force the async method to be synchronous<\/p>\n<pre class=\"\">_service.DoSomethingAsync().GetAwaiter().GetResult(); \n<\/pre>\n<p>I found myself having to do this a few times in my code, so thought I should create an extension to simplify this..<\/p>\n<h2>The solution<\/h2>\n<p>Create a class &#8220;TaskExtension.cs&#8221;, and add the following code<\/p>\n<pre>public static class TaskExtension\n{\n\tpublic static T AwaitResult(this System.Threading.Tasks.Task task)\n\t{\n\t  return task.GetAwaiter().GetResult();\n\t}\n}\n<\/pre>\n<h3>Example<\/h3>\n<p>This is how you can use it..<\/p>\n<pre class=\"wp-block-preformatted\">_service.DoSomethingAsync().AwaitResult();<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>c# Extension to Await a result when performing Async coding<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1504","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/1504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=1504"}],"version-history":[{"count":0,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/1504\/revisions"}],"wp:attachment":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=1504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=1504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=1504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}