{"id":2820,"date":"2022-02-14T18:06:26","date_gmt":"2022-02-14T10:06:26","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=2820"},"modified":"2022-02-16T17:18:08","modified_gmt":"2022-02-16T09:18:08","slug":"asp-net-core-asynchronous","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2022\/02\/14\/asp-net-core-asynchronous\/","title":{"rendered":"Asp.Net Core\u7684\u975e\u540c\u6b65\u554f\u984c"},"content":{"rendered":"<p>\u6700\u8fd1\u5728\u5beb Asp.Net Core \u7684 Api \u6642\u9047\u5230\u4e00\u500b\u975e\u540c\u6b65\u7684\u554f\u984c\uff0c\u5728\u89e3\u6c7a\u4e4b\u5f8c\u771f\u7684\u89ba\u5f97\u51fa\u4e4e\u610f\u6599\uff0c\u6240\u4ee5\u5beb\u4e0b\u4f86\u8ddf\u5927\u5bb6\u5206\u4eab\u4e00\u4e0b\uff0c\u4ee5\u53ca\u907f\u514d\u4ee5\u5f8c\u518d\u9047\u5230\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u751a\u9ebc\u662f\u975e\u540c\u6b65<\/h2>\n<p>\u5728\u89e3\u91cb\u751a\u9ebc\u662f\u975e\u540c\u6b65\u4e4b\u524d\u5148\u8b93\u5927\u5bb6\u8a8d\u8b58\u4e00\u4e0b\u540c\u6b65\u7684\u5b9a\u7fa9\u662f\u751a\u9ebc\uff0c\u7c21\u55ae\u8aaa\u5c31\u662f\u4e00\u500b\u4efb\u52d9\u500b\u4efb\u52d9\u63a5\u7e8c\u7684\u505a\u4e0b\u53bb\uff0c\u5728\u505a\u5b8c\u524d\u4e00\u500b\u4efb\u52d9\u524d\u4e0d\u6703\u505a\u4e0b\u4e00\u500b\u4efb\u52d9\uff0c\u7c21\u55ae\u4f86\u8aaa\u5982\u4e0b\u5716\u6240\u793a\u3002<\/p>\n<pre><code class=\"language-mermaid\">flowchart LR\nA(TASK A) --&gt; B(TASK B)--&gt;C(TASK C) --&gt; D(TASK D) --&gt; E(TASK E)<\/code><\/pre>\n<p>\u81f3\u65bc\u975e\u540c\u6b65\u4f86\u8aaa\u5c31\u662f\u53ef\u4ee5\u6709\u591a\u500b\u4efb\u52d9\u540c\u6642\u9032\u884c\uff0c\u4e0d\u6703\u56e0\u70ba\u540c\u6642\u9032\u884c\u7684\u4efb\u52d9\u5c1a\u672a\u5b8c\u6210\u800c\u9700\u8981\u7b49\u5f85\u7684\u72c0\u6cc1\uff0c\u5c31\u5982\u4e0b\u5716\u6240\u793a\u3002<\/p>\n<pre><code class=\"language-mermaid\">flowchart LR\nA(TASK A) --&gt; B(TASK B)\nC(TASK C) --&gt; D(TASK D) --&gt; E(TASK E)\nF(TASK F) --&gt; G(TASK G)<\/code><\/pre>\n<h2>\u6240\u4ee5\u9047\u5230\u751a\u9ebc\u554f\u984c<\/h2>\n<p>\u9047\u5230\u7684\u554f\u984c\u662f\u5728\u65bc\u9047\u5230\u9ad8\u4f75\u767c\u7684\u8acb\u6c42\u6642\uff0c Api \u90fd\u6703\u5df2\u903e\u6642\u4f86\u53cd\u61c9\u9019\u500b\u8acb\u6c42\uff0c\u7d93\u904e\u4e00\u756a\u7684\u4fee\u6539\u4ee5\u53ca\u4ea4\u53c9\u6bd4\u5c0d\u5f8c\u767c\u73fe\uff0c\u5df2\u540c\u6b65\u7684\u5beb\u6cd5\u4f86\u64b0\u5beb Api \u9047\u4e0a\u9700\u8981\u8207\u5176\u4ed6 Api \u6e9d\u901a\u6642\u5982\u679c\u5728\u9ad8\u4f75\u767c\u7684\u72c0\u6cc1\u4e0b\u6703\u9020\u6210\u6211\u5011\u7684 Api \u518d\u4e5f\u6c92\u6709\u53cd\u61c9\u7684\u72c0\u6cc1\uff0c\u518d\u4f86\u5c31\u662f\u7576\u4ee5\u932f\u8aa4\u7684\u975e\u540c\u6b65\u5beb\u6cd5\u4e0b\u4e5f\u6703\u9020\u6210\u56b4\u91cd\u7684\u903e\u6642\u72c0\u6cc1\u767c\u751f\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u518d\u9032\u4e00\u6b65\u7684\u8aaa\u660e\u3002<\/p>\n<h3>Asp.Net Core \u975e\u540c\u6b65\u7684\u5beb\u6cd5<\/h3>\n<p>\u5728 <code>.Net Framework 4.5<\/code> \u4e4b\u5f8c\u591a\u4e86 <code>async<\/code> \u8ddf <code>await<\/code> \u7684\u8a9e\u6cd5\u7cd6\uff0c\u53ea\u8981\u5728\u60f3\u8981\u4f5c\u70ba\u975e\u540c\u6b65\u7684\u65b9\u6cd5\u56de\u50b3\u503c\u524d\u5ba3\u544a <code>async<\/code> \u4e26\u642d\u914d <code>Task&lt;T&gt;<\/code> \u5373\u53ef\u5b8c\u6210\u975e\u540c\u6b65\u65b9\u6cd5\u7684\u8d77\u624b\u5f0f\uff0c\u63a5\u4e0b\u4f86\u5982\u679c\u662f\u8981\u540c\u6b65\u7b49\u5f85\u7d50\u679c\u7684\u65b9\u6cd5\u547c\u53eb\u524d\u4f7f\u7528 <code>await<\/code> \u5373\u53ef\u5b8c\u6210\uff0c\u82e5\u662f\u4e0d\u9700\u7b49\u5f85\u6642\u5c31\u4e0d\u8981\u52a0 <code>await<\/code> \u7684\u95dc\u9375\u5b57\u5373\u53ef\u3002\u5b8c\u6574\u4f8b\u5b50\u5982\u4e0b\u5217\u6240\u5f0f\u3002<\/p>\n<pre><code class=\"language-csharp\">    public async Task&lt;IEnumerable&lt;string&gt;&gt; GetStringAsync()\n    {\n        await Task.Delay(TimeSpan.FromSeconds(10));\n        return new string[]{&quot;First&quot;, &quot;Second&quot;, &quot;Third&quot;};\n    }<\/code><\/pre>\n<h3>\u9047\u5230\u7684\u554f\u984c<\/h3>\n<p>\u5982\u679c\u662f\u5f9e <code>.Net Framework 4.5<\/code> \u4e4b\u524d\u5beb\u4e0a\u4f86\u7684\u670b\u53cb\u5011\u53ef\u80fd\u6703\u5beb\u51fa\u540c\u6b65\u7684\u8a9e\u6cd5\uff0c\u4f46\u662f\u540c\u6b65\u7684\u8a9e\u6cd5\u9047\u5230\u9ad8\u4f75\u767c\u7684\u8acb\u6c42\u518d\u52a0\u4e0a\u672c\u8eab\u9700\u8981\u8907\u96dc\u7684\u904b\u7b97\u6216\u5f0f\u9700\u8981\u8207\u5176\u4ed6 Api \u6e9d\u901a\u6642\u5c31\u6703\u9020\u6210\u672c\u8eab\u7684\u963b\u585e\uff0c\u751a\u81f3\u65bc\u5c0e\u81f4\u65bc\u6574\u500b\u7db2\u7ad9\u6c92\u6709\u56de\u61c9\u7684\u7a98\u5883\u3002<br \/>\n\u518d\u4f86\u540c\u6b65\u7684 Api \u547c\u53eb\u5230\u975e\u540c\u6b65\u7684\u8a9e\u6cd5\u6642\u6c92\u8fa6\u6cd5\u4f7f\u7528 <code>await<\/code> \u7684\u95dc\u9375\u5b57\uff0c\u6240\u4ee5\u53ea\u80fd\u4f7f\u7528 <code>Task.Result<\/code> \u7684\u65b9\u5f0f\u4f86\u547c\u53eb\uff0c\u5982\u4e0b\u5716\u6240\u793a\u3002<\/p>\n<pre><code class=\"language-cs\">    [HttpGet(&quot;Result&quot;)]\n    public IEnumerable&lt;string&gt; GetResult()\n    {\n        return _service.GetStringAsync().Result;\n    }<\/code><\/pre>\n<p>\u4e0d\u904e\u540c\u6b65\u7684\u8a9e\u6cd5\u9047\u5230\u9ad8\u4f75\u767c\u7684\u8acb\u6c42\u6703\u9020\u6210\u53cd\u61c9\u903e\u6642\u7684\u72c0\u6cc1\uff0c\u6240\u4ee5\u9019\u6642\u6703\u4f7f\u7528\u975e\u540c\u6b65\u7684\u8a9e\u6cd5\u4f86\u6539\u5beb\uff0c\u4f46\u662f\u55ae\u7d14\u7684\u52a0\u4e0a <code>async<\/code> \u800c\u642d\u914d <code>Task.Result<\/code> \u9084\u662f\u6703\u9020\u6210\u963b\u585e\u7684\u72c0\u6cc1\uff0c\u9019\u662f\u722c\u6587\u770b\u5230<a href=\"https:\/\/docs.microsoft.com\/zh-tw\/dotnet\/csharp\/async\" title=\"\u975e\u540c\u6b65\u7a0b\u5f0f\u8a2d\u8a08\" rel=\"nofollow noopener\" target=\"_blank\">MSDN\u975e\u540c\u6b65\u7a0b\u5f0f\u8a2d\u8a08<\/a>\u624d\u77e5\u9053\u6709\u9019\u6a23\u7684\u7d50\u679c\uff0c\u6240\u4ee5\u4e0b\u5217\u7684\u5beb\u6cd5\u4ecd\u7136\u6703\u6709\u9020\u6210\u8acb\u6c42\u963b\u585e\u7684\u72c0\u6cc1\u3002<\/p>\n<pre><code class=\"language-cs\">    [HttpGet(&quot;Result&quot;)]\n    public async Task&lt;IEnumerable&lt;string&gt;&gt; GetResult()\n    {\n        return _service.GetStringAsync().Result;\n    }<\/code><\/pre>\n<p>\u6240\u4ee5\u7a76\u7adf\u8a72\u5982\u4f55\u5beb\u624d\u662f\u6b63\u78ba\u7684\u5beb\u6cd5\u5462?<\/p>\n<h3>\u6b63\u78ba\u7684\u5beb\u6cd5<\/h3>\n<p>\u6240\u4ee5\u7d9c\u5408\u4ee5\u4e0a\u7684\u5167\u5bb9\uff0c\u8981\u5beb\u4e00\u500b\u975e\u540c\u6b65\u7684 Api \u57fa\u672c\u4e0a\u8981\u5148\u4f7f\u7528 <code>async<\/code> \u9019\u500b\u95dc\u9375\u5b57\uff0c\u63a5\u4e0b\u4f86\u91dd\u5c0d\u6709\u6c92\u6709\u56de\u50b3\u503c\u4f7f\u7528 <code>Task&lt;T&gt;<\/code> \u6216\u662f <code>Task<\/code> \u7684\u8fd4\u9084\u503c\uff0c\u5982\u4e0b\u5217\u6240\u5f0f\u3002<\/p>\n<pre><code class=\"language-cs\">    [HttpGet(&quot;String&quot;)]\n    public async Task&lt;IEnumerable&lt;string&gt;&gt; GetString()\n    {\n        await Task.Delay(TimeSpan.FromSeconds(10));\n        return new string[]{&quot;First&quot;, &quot;Second&quot;, &quot;Third&quot;};\n    }<\/code><\/pre>\n<p>\u9664\u6b64\u4e4b\u5916\uff0c\u9047\u5230\u9700\u8981\u963b\u585e\u7684\u65b9\u6cd5\u6642\uff0c\u4e5f\u5c31\u662f\u9047\u5230\u9700\u8981\u7b49\u5f85\u5176\u4ed6\u65b9\u6cd5\u7684\u7d50\u679c\u6642\u8981\u4f7f\u7528 <code>await<\/code> \u7684\u95dc\u9375\u5b57\uff0c\u5982\u4e0b\u5217\u6240\u5f0f\uff0c<\/p>\n<pre><code class=\"language-cs\">    [HttpGet(&quot;Result&quot;)]\n    public async Task&lt;IEnumerable&lt;string&gt;&gt; GetResult()\n    {\n        return await _service.GetStringAsync();\n    }<\/code><\/pre>\n<p>\u9019\u88e1\u5343\u842c\u6ce8\u610f\u4e0d\u8981\u4f7f\u7528 <code>Task.Result<\/code> \u9019\u6a23\u7684\u5beb\u6cd5\u5426\u5247\u904b\u884c\u7684\u53cd\u61c9\u6642\u9593\u6703\u5927\u6253\u6298\u6263 \u3002<\/p>\n<h3>\u6e2c\u8a66\u7684\u7d50\u679c<\/h3>\n<p>\u4e0d\u8ad6\u662f\u4ee5\u540c\u6b65\u7684\u5beb\u6cd5\uff0c\u6216\u662f\u5728\u975e\u540c\u6b65\u7684 Api \u4e2d\u547c\u53eb <code>Task.Result<\/code> \u7684\u5beb\u6cd5\uff0c\u7576\u9047\u5230\u6bcf\u79d2100\u6b21\u7684\u9ad8\u4f75\u767c\u547c\u53eb\u6642\u7684\u7d50\u679c\u90fd\u6703\u4ee5\u8d85\u904e\u4e94\u79d2\u4ee5\u4e0a\u7684\u5ef6\u9072\u4f86\u53cd\u61c9\uff0c\u53ea\u6709\u7528 <code>async \/ await<\/code> \u7684\u5beb\u6cd5\u53ef\u4ee5\u5728\u4e09\u79d2\u5167\u6b63\u5e38\u53cd\u61c9\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u7d93\u904e\u9019\u6b21\u7684\u7d93\u9a57\u5728\u64b0\u5beb Api \u6642\u5c31\u5148\u628a <code>async<\/code> \u5148\u5beb\u597d\uff0c\u5982\u6b64\u53ef\u4ee5\u907f\u514d\u9ad8\u4f75\u767c\u6642\u9020\u6210 Api \u7684\u53cd\u61c9\u88ab\u963b\u585e\u4f4f\u5c0e\u81f4\u65bc\u5f8c\u9762\u7684\u5de5\u4f5c\u90fd\u7121\u6cd5\u9032\u884c\u7684\u7a98\u5883\uff0c\u518d\u4f86\u5c31\u662f <code>\u975e\u540c\u6b65<\/code> \u7684\u5beb\u6cd5\u90fd\u8981\u4ee5 <code>aync<\/code> \u53ca <code>await<\/code> \u4f86\u64b0\u5beb\uff0c\u907f\u514d\u5beb\u51fa <code>Task.Result<\/code> \u7684\u5beb\u6cd5\uff0c\u5982\u6b64\u53ef\u4ee5\u907f\u514d\u963b\u585e\u7684\u72c0\u6cc1\u767c\u751f\u3002<\/p>\n<h2>\u53c3\u8003\u8cc7\u6599<\/h2>\n<ol>\n<li><a href=\"https:\/\/docs.microsoft.com\/zh-tw\/dotnet\/csharp\/async\" title=\"\u975e\u540c\u6b65\u7a0b\u5f0f\u8a2d\u8a08\" rel=\"nofollow noopener\" target=\"_blank\">MSDN\u975e\u540c\u6b65\u7a0b\u5f0f\u8a2d\u8a08<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u5beb Asp.Net Core \u7684 Api \u6642\u9047\u5230\u4e00\u500b\u975e\u540c\u6b65\u7684\u554f\u984c\uff0c\u5728\u89e3\u6c7a\u4e4b\u5f8c\u771f\u7684\u89ba\u5f97\u51fa\u4e4e\u610f\u6599\uff0c\u6240\u4ee5\u5beb\u4e0b\u4f86 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2022\/02\/14\/asp-net-core-asynchronous\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Asp.Net Core\u7684\u975e\u540c\u6b65\u554f\u984c\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[102,92],"class_list":["post-2820","post","type-post","status-publish","format-standard","hentry","category-develop","tag-async","tag-dotnetcore"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2820","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=2820"}],"version-history":[{"count":67,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2820\/revisions"}],"predecessor-version":[{"id":2851,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2820\/revisions\/2851"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=2820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=2820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=2820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}