{"id":633,"date":"2025-12-07T15:46:25","date_gmt":"2025-12-07T07:46:25","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=633"},"modified":"2025-12-07T15:46:25","modified_gmt":"2025-12-07T07:46:25","slug":"net-core%e9%a1%b9%e7%9b%ae%e4%b8%ad%e8%87%aa%e5%ae%9a%e4%b9%89%e6%9c%8d%e5%8a%a1%e7%9a%84%e5%ae%9e%e7%8e%b0%e6%ad%a5%e9%aa%a4","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/net-core%e9%a1%b9%e7%9b%ae%e4%b8%ad%e8%87%aa%e5%ae%9a%e4%b9%89%e6%9c%8d%e5%8a%a1%e7%9a%84%e5%ae%9e%e7%8e%b0%e6%ad%a5%e9%aa%a4\/","title":{"rendered":".net\u00a0core\u9879\u76ee\u4e2d\u81ea\u5b9a\u4e49\u670d\u52a1\u7684\u5b9e\u73b0\u6b65\u9aa4"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd <strong>\u6700\u65b0\u3001\u6700\u6e05\u6670\u3001\u53ef\u76f4\u63a5\u7528\u4e8e\u5199\u6280\u672f\u6587\u7ae0\u6216\u6559\u7a0b\u7684\u300a.NET Core \u9879\u76ee\u4e2d\u81ea\u5b9a\u4e49\u670d\u52a1\u7684\u5b9e\u73b0\u6b65\u9aa4\u300b<\/strong>\u3002<br>\u5185\u5bb9\u5305\u62ec\uff1a<strong>\u57fa\u7840\u6b65\u9aa4 + \u4ee3\u7801\u793a\u4f8b + \u63a8\u8350\u6700\u4f73\u5b9e\u8df5<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u4e00\u3001\u4ec0\u4e48\u662f\u81ea\u5b9a\u4e49\u670d\u52a1\uff1f<\/h1>\n\n\n\n<p>\u5728 .NET Core \u4e2d\uff0c\u6240\u6709\u4e1a\u52a1\u903b\u8f91\u90fd\u5e94\u8be5\u901a\u8fc7 <strong>\u4f9d\u8d56\u6ce8\u5165\uff08DI\uff09<\/strong> \u7684\u65b9\u5f0f\u7ba1\u7406\u3002<br>\u81ea\u5b9a\u4e49\u670d\u52a1\u5c31\u662f\u4f60\u5199\u7684\u7c7b\uff08\u4e1a\u52a1\u903b\u8f91\u7c7b\uff09\uff0c\u901a\u8fc7 DI \u6ce8\u518c\u7ed9\u6846\u67b6\uff0c\u7136\u540e\u5728\u63a7\u5236\u5668\u7b49\u5730\u65b9\u4f7f\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u4e8c\u3001\u81ea\u5b9a\u4e49\u670d\u52a1\u7684\u5b8c\u6574\u5b9e\u73b0\u6b65\u9aa4\uff085 \u6b65\uff09<\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>\u6b65\u9aa4 1\uff1a\u521b\u5efa\u670d\u52a1\u63a5\u53e3\uff08I \u63a5\u53e3\u5c42\uff09<\/strong><\/h1>\n\n\n\n<p>\ud83d\udccc \u5efa\u8bae\u653e\u5230 <code>\/Services<\/code> \u6587\u4ef6\u5939\u4e2d\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic interface IEmailService\n{\n    void Send(string to, string subject, string content);\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>\u6b65\u9aa4 2\uff1a\u521b\u5efa\u670d\u52a1\u5b9e\u73b0\u7c7b<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class EmailService : IEmailService\n{\n    public void Send(string to, string subject, string content)\n    {\n        Console.WriteLine($&quot;Send email to {to}, subject={subject}&quot;);\n    }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>\u6b65\u9aa4 3\uff1a\u5728 Program.cs \u6ce8\u518c\u670d\u52a1\uff08DI \u5bb9\u5668\uff09<\/strong><\/h1>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>.NET 6 \/ 7 \/ 8 \u7684\u98ce\u683c\uff1a<\/p>\n<\/blockquote>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nbuilder.Services.AddScoped&amp;lt;IEmailService, EmailService&gt;();\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udccc \u53ef\u9009\uff1a\u6ce8\u518c\u65b9\u5f0f\u5bf9\u7167<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u4f5c\u7528\u8303\u56f4<\/th><\/tr><\/thead><tbody><tr><td><code>AddSingleton<\/code><\/td><td>\u5e94\u7528\u8fd0\u884c\u671f\u95f4\u53ea\u521b\u5efa\u4e00\u6b21\u5b9e\u4f8b<\/td><\/tr><tr><td><code>AddScoped<\/code><\/td><td>\u6bcf\u4e2a HTTP \u8bf7\u6c42\u521b\u5efa\u4e00\u6b21\u5b9e\u4f8b\uff08Web \u9879\u76ee\u6700\u5e38\u7528\uff09<\/td><\/tr><tr><td><code>AddTransient<\/code><\/td><td>\u6bcf\u6b21\u6ce8\u5165\u90fd\u521b\u5efa\u65b0\u5b9e\u4f8b<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>\u6b65\u9aa4 4\uff1a\u5728 Controller \u6216\u5176\u4ed6\u670d\u52a1\u4e2d\u6ce8\u5165\u4f7f\u7528<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;ApiController]\n&#x5B;Route(&quot;&#x5B;controller]&quot;)]\npublic class TestController : ControllerBase\n{\n    private readonly IEmailService _emailService;\n\n    public TestController(IEmailService emailService)\n    {\n        _emailService = emailService;\n    }\n\n    &#x5B;HttpGet(&quot;send&quot;)]\n    public IActionResult SendTest()\n    {\n        _emailService.Send(&quot;abc@test.com&quot;, &quot;Hello&quot;, &quot;This is test&quot;);\n        return Ok(&quot;Email Sent&quot;);\n    }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>\u6b65\u9aa4 5\uff1a\u8fd0\u884c\u9879\u76ee\u9a8c\u8bc1 DI \u662f\u5426\u751f\u6548<\/strong><\/h1>\n\n\n\n<p>\u8bbf\u95ee\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nGET \/test\/send\n\n<\/pre><\/div>\n\n\n<p>\u63a7\u5236\u53f0\u5e94\u8f93\u51fa\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSend email to abc@test.com, subject=Hello\n\n<\/pre><\/div>\n\n\n<p>DI \u6210\u529f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u4e09\u3001\u53ef\u9009\u8fdb\u9636\uff1a\u4f7f\u7528 Keyed Services\uff08.NET 8 \u65b0\u7279\u6027\uff09<\/h1>\n\n\n\n<p>\u5047\u8bbe\u4f60\u6709\u4e24\u4e2a\u90ae\u4ef6\u670d\u52a1\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nbuilder.Services.AddKeyedScoped&amp;lt;IEmailService, EmailService&gt;(&quot;normal&quot;);\nbuilder.Services.AddKeyedScoped&amp;lt;IEmailService, PremiumEmailService&gt;(&quot;vip&quot;);\n\n<\/pre><\/div>\n\n\n<p>\u4f7f\u7528\u65b9\u5f0f\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class TestController : ControllerBase\n{\n    private readonly IEmailService _emailService;\n\n    public TestController(&#x5B;FromKeyedServices(&quot;vip&quot;)] IEmailService emailService)\n    {\n        _emailService = emailService;\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udc49 \u53ef\u4ee5\u7528\u4e8e\u591a\u79df\u6237\u3001\u7b56\u7565\u3001\u4e0d\u540c\u4e1a\u52a1\u5206\u7ec4\uff0c\u975e\u5e38\u5f3a\u5927\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u56db\u3001\u6700\u4f73\u5b9e\u8df5\u603b\u7ed3\uff08\u9002\u5408\u653e\u6587\u7ae0\u7ed3\u5c3e\uff09<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4e1a\u52a1\u903b\u8f91\u5168\u90e8\u5c01\u88c5\u5230\u670d\u52a1\u4e2d<\/strong>\uff08Controller \u4fdd\u6301\u7cbe\u7b80\uff09<\/li>\n\n\n\n<li>\u670d\u52a1\u5fc5\u987b\u4f7f\u7528 <strong>\u63a5\u53e3 + \u5b9e\u73b0\u7c7b<\/strong> \u8bbe\u8ba1\uff08\u4fbf\u4e8e\u6d4b\u8bd5\u548c\u66ff\u6362\uff09<\/li>\n\n\n\n<li>\u6839\u636e\u751f\u547d\u5468\u671f\u9009\u62e9\u6ce8\u518c\u65b9\u5f0f\uff08Scoped \u6700\u5e38\u7528\uff09<\/li>\n\n\n\n<li>.NET 8 \u9879\u76ee\u63a8\u8350\u4f7f\u7528 <strong>Keyed Services<\/strong> \u4f18\u5316\u591a\u5b9e\u4f8b\u573a\u666f<\/li>\n\n\n\n<li>\u907f\u514d\u670d\u52a1\u4e2d\u6ce8\u5165\u592a\u591a\u4f9d\u8d56\uff0c\u5efa\u8bae\u62c6\u5206\u4e1a\u52a1\uff08SRP \u539f\u5219\uff09<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd \u6700\u65b0\u3001\u6700\u6e05\u6670\u3001\u53ef\u76f4\u63a5\u7528\u4e8e\u5199\u6280\u672f\u6587\u7ae0\u6216\u6559\u7a0b\u7684\u300a.NET Core \u9879&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/net-core%e9%a1%b9%e7%9b%ae%e4%b8%ad%e8%87%aa%e5%ae%9a%e4%b9%89%e6%9c%8d%e5%8a%a1%e7%9a%84%e5%ae%9e%e7%8e%b0%e6%ad%a5%e9%aa%a4\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[],"class_list":["post-633","post","type-post","status-publish","format-standard","hentry","category-asp-net"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=633"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/633\/revisions"}],"predecessor-version":[{"id":634,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/633\/revisions\/634"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}