{"id":56500,"date":"2025-08-14T19:34:34","date_gmt":"2025-08-14T11:34:34","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/56500.html"},"modified":"2025-08-14T19:34:34","modified_gmt":"2025-08-14T11:34:34","slug":"c-%e9%ab%98%e5%b9%b6%e5%8f%91%e5%a4%84%e7%90%86%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/56500.html","title":{"rendered":"C# \u9ad8\u5e76\u53d1\u5904\u7406\u65b9\u5f0f"},"content":{"rendered":"<h4>1. \u5f02\u6b65\u7f16\u7a0b&#xff08;async\/await&#xff09;<\/h4>\n<p>\u6838\u5fc3\u4f5c\u7528&#xff1a;\u5f02\u6b65\u7f16\u7a0b\u662f\u5904\u7406\u9ad8\u5e76\u53d1\u7684\u4e00\u79cd\u975e\u5e38\u6709\u6548\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u4f7f\u7528async\u548cawait\u5173\u952e\u5b57&#xff0c;\u4f60\u53ef\u4ee5\u7f16\u5199\u975e\u963b\u585e\u7684\u4ee3\u7801&#xff0c;\u4ece\u800c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u6027\u548c\u541e\u5410\u91cf\u3002<\/p>\n<p>public async Task&lt;ActionResult&gt; GetData()<br \/>\n{<br \/>\n    var data &#061; await _dbContext.Data.ToListAsync(); \/\/ \u5f02\u6b65\u6570\u636e\u5e93\u64cd\u4f5c<br \/>\n    return Ok(data);<br \/>\n}<\/p>\n<ul>\n<li>\n<p>\u5173\u952e\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6240\u6709 I\/O \u64cd\u4f5c&#xff08;\u6570\u636e\u5e93\u3001\u7f51\u7edc\u3001\u6587\u4ef6&#xff09;\u5fc5\u987b\u5f02\u6b65<\/p>\n<\/li>\n<li>\n<p>\u907f\u514d\u00a0Task.Wait()\u00a0\u6216\u00a0Task.Result&#xff08;\u4f1a\u5bfc\u81f4\u6b7b\u9501&#xff09;<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>2. \u5206\u5e03\u5f0f\u67b6\u6784<\/h4>\n<h5>(1) \u6d88\u606f\u961f\u5217&#xff08;\u524a\u5cf0\u586b\u8c37&#xff09;<\/h5>\n<p>\/\/ \u4f7f\u7528 RabbitMQ.Client \u53d1\u9001<br \/>\nusing var channel &#061; _connection.CreateModel();<br \/>\nchannel.QueueDeclare(&#034;orders&#034;);<br \/>\nvar body &#061; Encoding.UTF8.GetBytes(orderJson);<br \/>\nchannel.BasicPublish(&#034;&#034;, &#034;orders&#034;, body); \/\/ \u5f02\u6b65\u89e3\u8026<\/p>\n<ul>\n<li>\n<p>\u63a8\u8350\u5de5\u5177&#xff1a;RabbitMQ\u3001Kafka\u3001Azure Service Bus<\/p>\n<\/li>\n<\/ul>\n<h5>(2) \u5206\u5e03\u5f0f\u7f13\u5b58<\/h5>\n<p>\/\/ \u4f7f\u7528 StackExchange.Redis<br \/>\nIDatabase cache &#061; Connection.GetDatabase();<br \/>\nawait cache.StringSetAsync(&#034;key&#034;, &#034;value&#034;, TimeSpan.FromMinutes(10));<br \/>\nstring value &#061; await cache.StringGetAsync(&#034;key&#034;);<\/p>\n<ul>\n<li>\n<p>\u63a8\u8350\u5de5\u5177&#xff1a;Redis\u3001Memcached<\/p>\n<\/li>\n<\/ul>\n<h4 style=\"background-color:transparent\">3. \u6570\u636e\u5e93\u4f18\u5316<\/h4>\n<h5>(1) \u8bfb\u5199\u5206\u79bb<\/h5>\n<p>(2) \u5206\u5e93\u5206\u8868&#xff1a;\u4f7f\u7528 ShardingCore \u7b49\u5e93<\/p>\n<h4>\u67b6\u6784\u9009\u62e9\u5efa\u8bae<\/h4>\n<table>\n<tr>\u573a\u666f\u63a8\u8350\u65b9\u6848<\/tr>\n<tbody>\n<tr>\n<td>CPU \u5bc6\u96c6\u578b<\/td>\n<td>TPL Dataflow \/ Parallel.ForEach<\/td>\n<\/tr>\n<tr>\n<td>I\/O \u5bc6\u96c6\u578b<\/td>\n<td>async\/await &#043; \u6d88\u606f\u961f\u5217<\/td>\n<\/tr>\n<tr>\n<td>\u5b9e\u65f6\u6570\u636e\u5904\u7406<\/td>\n<td>SignalR &#043; WebSocket<\/td>\n<\/tr>\n<tr>\n<td>\u5927\u6570\u636e\u5904\u7406<\/td>\n<td>Actor \u6a21\u578b&#xff08;Akka.NET&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>4. \u4f7f\u7528\u9501&#xff08;lock&#xff09;\u548c\u4e92\u65a5\u91cf&#xff08;Mutex&#xff09;<\/h4>\n<p>\u867d\u7136\u9501\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u53ef\u80fd\u4f1a\u5f15\u8d77\u6027\u80fd\u74f6\u9888&#xff0c;\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4ecd\u7136\u9700\u8981\u4f7f\u7528\u3002\u786e\u4fdd\u53ea\u5728\u5fc5\u8981\u65f6\u4f7f\u7528&#xff0c;\u5e76\u5c3d\u53ef\u80fd\u51cf\u5c11\u9501\u5b9a\u8303\u56f4\u3002<\/p>\n<p>private object lockObject &#061; new object();<br \/>\npublic void ProcessData(int data)<br \/>\n{<br \/>\n    lock (lockObject)<br \/>\n    {<br \/>\n        \/\/ \u6267\u884c\u9700\u8981\u540c\u6b65\u7684\u4ee3\u7801\u5757<br \/>\n    }<br \/>\n}<\/p>\n<h4>5. \u4f7f\u7528\u7ebf\u7a0b\u6c60&#xff08;ThreadPool&#xff09;\u548cTask.Run<\/h4>\n<p>\u5bf9\u4e8e\u9700\u8981\u5927\u91cf\u5e76\u53d1\u7ebf\u7a0b\u7684\u573a\u666f&#xff0c;\u53ef\u4ee5\u4f7f\u7528Task.Run\u6765\u542f\u52a8\u4e00\u4e2a\u65b0\u4efb\u52a1\u5230\u7ebf\u7a0b\u6c60\u4e2d\u3002\u8fd9\u6bd4\u624b\u52a8\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u66f4\u9ad8\u6548\u3002<\/p>\n<p>Task[] tasks &#061; new Task[10];<br \/>\nfor (int i &#061; 0; i &lt; 10; i&#043;&#043;)<br \/>\n{<br \/>\n    tasks[i] &#061; Task.Run(() &#061;&gt; DoWork());<br \/>\n}<br \/>\nawait Task.WhenAll(tasks);<\/p>\n<h4>\u5178\u578b\u8bef\u533a<\/h4>\n<li>\n<p>\u8fc7\u5ea6\u4f7f\u7528\u00a0Task.Run&#xff1a;I\/O \u64cd\u4f5c\u76f4\u63a5\u7528\u00a0async\u00a0\u800c\u975e\u5c01\u88c5\u7ebf\u7a0b<\/p>\n<\/li>\n<li>\n<p>\u5168\u5c40\u9759\u6001\u9501&#xff1a;\u5bfc\u81f4\u65e0\u8c13\u7684\u7ebf\u7a0b\u963b\u585e<\/p>\n<\/li>\n<li>\n<p>\u540c\u6b65\u8c03\u7528\u5f02\u6b65\u65b9\u6cd5&#xff1a;GetAwaiter().GetResult()\u00a0\u5f15\u53d1\u6b7b\u9501<\/p>\n<\/li>\n<li>\n<p>\u5ffd\u7565\u8fde\u63a5\u6c60\u914d\u7f6e&#xff1a;\u6570\u636e\u5e93\u8fde\u63a5\u6210\u4e3a\u74f6\u9888<\/p>\n<\/li>\n<p>&#x1f4a1;\u00a0\u9ec4\u91d1\u6cd5\u5219&#xff1a;<br \/>\n\u5f02\u6b65\u5316\u6240\u6709 I\/O \u8def\u5f84 &#043; \u907f\u514d\u5171\u4eab\u72b6\u6001 &#043; \u961f\u5217\u89e3\u8026<br \/>\n\u901a\u8fc7\u8d1f\u8f7d\u6d4b\u8bd5&#xff08;\u5982 JMeter\/LoadRunner&#xff09;\u6301\u7eed\u9a8c\u8bc1\u7cfb\u7edf\u6781\u9650\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb136\u6b21\uff0c\u70b9\u8d5e4\u6b21\uff0c\u6536\u85cf2\u6b21\u3002C# \u9ad8\u5e76\u53d1\u5904\u7406\u65b9\u5f0f<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,768,100],"topic":[],"class_list":["post-56500","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-sql","tag-100"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C# \u9ad8\u5e76\u53d1\u5904\u7406\u65b9\u5f0f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/56500.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# \u9ad8\u5e76\u53d1\u5904\u7406\u65b9\u5f0f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb136\u6b21\uff0c\u70b9\u8d5e4\u6b21\uff0c\u6536\u85cf2\u6b21\u3002C# \u9ad8\u5e76\u53d1\u5904\u7406\u65b9\u5f0f\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/56500.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-14T11:34:34+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/56500.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/56500.html\",\"name\":\"C# \u9ad8\u5e76\u53d1\u5904\u7406\u65b9\u5f0f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-14T11:34:34+00:00\",\"dateModified\":\"2025-08-14T11:34:34+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/56500.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/56500.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/56500.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# \u9ad8\u5e76\u53d1\u5904\u7406\u65b9\u5f0f\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C# \u9ad8\u5e76\u53d1\u5904\u7406\u65b9\u5f0f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/56500.html","og_locale":"zh_CN","og_type":"article","og_title":"C# \u9ad8\u5e76\u53d1\u5904\u7406\u65b9\u5f0f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb136\u6b21\uff0c\u70b9\u8d5e4\u6b21\uff0c\u6536\u85cf2\u6b21\u3002C# \u9ad8\u5e76\u53d1\u5904\u7406\u65b9\u5f0f","og_url":"https:\/\/www.wsisp.com\/helps\/56500.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-14T11:34:34+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/56500.html","url":"https:\/\/www.wsisp.com\/helps\/56500.html","name":"C# \u9ad8\u5e76\u53d1\u5904\u7406\u65b9\u5f0f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-14T11:34:34+00:00","dateModified":"2025-08-14T11:34:34+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/56500.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/56500.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/56500.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C# \u9ad8\u5e76\u53d1\u5904\u7406\u65b9\u5f0f"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/56500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=56500"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/56500\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=56500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=56500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=56500"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=56500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}