{"id":609,"date":"2025-12-07T14:42:52","date_gmt":"2025-12-07T06:42:52","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=609"},"modified":"2025-12-07T14:42:52","modified_gmt":"2025-12-07T06:42:52","slug":"net-8-%e4%b8%ad%e7%9a%84-keyed-services%e8%a7%a3%e9%94%81%e4%be%9d%e8%b5%96%e6%b3%a8%e5%85%a5%e7%9a%84%e6%96%b0%e6%96%b9%e5%bc%8f%e6%9c%80%e6%96%b0%e6%8e%a8%e8%8d%90","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/net-8-%e4%b8%ad%e7%9a%84-keyed-services%e8%a7%a3%e9%94%81%e4%be%9d%e8%b5%96%e6%b3%a8%e5%85%a5%e7%9a%84%e6%96%b0%e6%96%b9%e5%bc%8f%e6%9c%80%e6%96%b0%e6%8e%a8%e8%8d%90\/","title":{"rendered":".NET\u00a08\u00a0\u4e2d\u7684\u00a0Keyed\u00a0Services\u89e3\u9501\u4f9d\u8d56\u6ce8\u5165\u7684\u65b0\u65b9\u5f0f(\u6700\u65b0\u63a8\u8350)"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u5199\u4e00\u7bc7 <strong>\u901a\u4fd7\u6613\u61c2\u3001\u53ef\u76f4\u63a5\u7528\u4e8e\u535a\u5ba2\/\u6280\u672f\u6587\u6863\u7684\u300a.NET 8 \u4e2d\u7684 Keyed Services\uff1a\u89e3\u9501\u4f9d\u8d56\u6ce8\u5165\u7684\u65b0\u65b9\u5f0f\uff08\u6700\u65b0\u63a8\u8350\uff09\u300b<\/strong>\u3002<br>\u5185\u5bb9\u5305\u542b\uff1a\u539f\u7406\u3001\u4f7f\u7528\u573a\u666f\u3001\u4ee3\u7801\u793a\u4f8b\u3001\u4e0e\u4f20\u7edf DI \u5bf9\u6bd4\u3001\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\ude80 .NET 8 \u4e2d\u7684 Keyed Services\uff1a\u89e3\u9501\u4f9d\u8d56\u6ce8\u5165\u7684\u65b0\u65b9\u5f0f\uff08\u6700\u65b0\u63a8\u8350\uff09<\/h1>\n\n\n\n<p>\u5728 .NET 8 \u4e2d\uff0c\u5fae\u8f6f\u6b63\u5f0f\u4e3a\u5185\u7f6e\u4f9d\u8d56\u6ce8\u5165\uff08Microsoft.Extensions.DependencyInjection\uff09\u5f15\u5165\u4e86 <strong>Keyed Services<\/strong>\u3002<br>\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5347\u7ea7\uff0c\u8ba9 .NET \u539f\u751f\u652f\u6301\u201c\u540c\u7c7b\u578b\u670d\u52a1\u7684\u591a\u5b9e\u73b0\u9009\u62e9\u201d\u3002<\/p>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4\uff1a<br><strong>Keyed Services = \u7ed9\u4f9d\u8d56\u6ce8\u5165\u7684\u670d\u52a1\u201c\u6253\u6807\u7b7e\u201d \u2192 \u540c\u65f6\u6ce8\u518c\u591a\u4e2a\u5b9e\u73b0 \u2192 \u6309 key \u83b7\u53d6\u7279\u5b9a\u5b9e\u4f8b\u3002<\/strong><\/p>\n\n\n\n<p>\u4ee5\u524d\u6211\u4eec\u9700\u8981\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u81ea\u5df1\u5199\u5de5\u5382<\/li>\n\n\n\n<li>\u6216\u4f7f\u7528 Scrutor \/ Autofac \u89e3\u51b3<\/li>\n\n\n\n<li>\u6216\u6ce8\u5165 IEnumerable \u518d\u624b\u52a8\u6311\u9009<\/li>\n<\/ul>\n\n\n\n<p>\u73b0\u5728\u53ea\u9700\u8981\u539f\u751f\u8bed\u6cd5\u5373\u53ef\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">1\ufe0f\u20e3 \u4e3a\u4ec0\u4e48\u9700\u8981 Keyed Services\uff1f<\/h1>\n\n\n\n<p>\u4e0b\u9762\u7684\u573a\u666f\u975e\u5e38\u5e38\u89c1\uff1a<\/p>\n\n\n\n<p>\u274c \u4f60\u6709\u591a\u4e2a\u5b9e\u73b0\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ninterface IPayment { void Pay(); }\n\nclass WechatPay : IPayment { \u2026 }\nclass Alipay   : IPayment { \u2026 }\nclass Paypal   : IPayment { \u2026 }\n\n<\/pre><\/div>\n\n\n<p>\u4ee5\u524d\u4f60\u60f3\u6839\u636e\u4e1a\u52a1\u89c4\u5219\u52a8\u6001\u9009\u62e9 Pay \u670d\u52a1\uff0c\u9700\u8981\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6ce8\u5165\u6240\u6709\u5b9e\u73b0 (<code>IEnumerable&lt;IPayment><\/code>)<\/li>\n\n\n\n<li>\u81ea\u5df1\u5199 switch OR \u67e5\u5b57\u5178<\/li>\n\n\n\n<li>\u6216\u81ea\u5df1\u5199\u5de5\u5382<\/li>\n<\/ul>\n\n\n\n<p>\u73b0\u5728\u53ea\u9700\u8981\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nbuilder.Services.AddKeyedTransient&amp;lt;IPayment, WechatPay&gt;(&quot;wechat&quot;);\nbuilder.Services.AddKeyedTransient&amp;lt;IPayment, Alipay&gt;(&quot;alipay&quot;);\nbuilder.Services.AddKeyedTransient&amp;lt;IPayment, Paypal&gt;(&quot;paypal&quot;);\n\n<\/pre><\/div>\n\n\n<p>\u7136\u540e\u6839\u636e key \u83b7\u53d6\u5373\u53ef\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class PayController\n{\n    private readonly IPayment _payment;\n\n    public PayController(&#x5B;FromKeyedServices(&quot;wechat&quot;)] IPayment payment)\n    {\n        _payment = payment;\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>\u8fd9\u5c31\u662f Keyed Services \u7684\u6838\u5fc3\u80fd\u529b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">2\ufe0f\u20e3 Keyed Services \u7684\u56db\u79cd\u6ce8\u518c\u65b9\u5f0f<\/h1>\n\n\n\n<p>.NET 8 \u5185\u7f6e\u56db\u79cd\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7c7b\u578b<\/th><th>\u65b9\u6cd5<\/th><\/tr><\/thead><tbody><tr><td>Transient<\/td><td>AddKeyedTransient<\/td><\/tr><tr><td>Scoped<\/td><td>AddKeyedScoped<\/td><\/tr><tr><td>Singleton<\/td><td>AddKeyedSingleton<\/td><\/tr><tr><td>\u5b9e\u4f8b\u6ce8\u518c<\/td><td>AddKeyedSingleton(key, instance)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nbuilder.Services.AddKeyedTransient&amp;lt;IPayment, WechatPay&gt;(&quot;wechat&quot;);\nbuilder.Services.AddKeyedTransient&amp;lt;IPayment, Alipay&gt;(&quot;alipay&quot;);\nbuilder.Services.AddKeyedScoped&amp;lt;IPayment, Paypal&gt;(&quot;paypal&quot;);\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\">3\ufe0f\u20e3 \u89e3\u6790 Keyed \u670d\u52a1\u7684\u4e09\u79cd\u65b9\u5f0f<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u2714 \u65b9\u5f0f 1\uff1a\u6784\u9020\u51fd\u6570\u6ce8\u5165 + <code>[FromKeyedServices]<\/code>\uff08\u6700\u5e38\u7528\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class PayController\n{\n    private readonly IPayment _payment;\n\n    public PayController(&#x5B;FromKeyedServices(&quot;alipay&quot;)] IPayment payment)\n    {\n        _payment = payment;\n    }\n}\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">\u2714 \u65b9\u5f0f 2\uff1a\u5de5\u5382\u89e3\u6790\uff08\u8fd0\u884c\u65f6\u6839\u636e\u6761\u4ef6\u52a8\u6001\u9009\u62e9\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class PaymentFactory\n{\n    private readonly IServiceProvider _sp;\n\n    public PaymentFactory(IServiceProvider sp)\n    {\n        _sp = sp;\n    }\n\n    public IPayment Get(string key)\n        =&gt; _sp.GetKeyedService&amp;lt;IPayment&gt;(key)!;\n}\n\n<\/pre><\/div>\n\n\n<p>\u6ce8\u5165\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvar payment = paymentFactory.Get(&quot;wechat&quot;);\npayment.Pay();\n\n<\/pre><\/div>\n\n\n<p>\u8fd9\u662f\u5b98\u65b9\u539f\u751f\u652f\u6301\u7684\u201c\u8f7b\u91cf\u7ea7\u7b56\u7565\u5de5\u5382\u201d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u2714 \u65b9\u5f0f 3\uff1aTryGetKeyedService\uff08\u66f4\u5b89\u5168\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nif (_sp.TryGetKeyedService&amp;lt;IPayment&gt;(&quot;paypal&quot;, out var pay))\n{\n    pay!.Pay();\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\">4\ufe0f\u20e3 \u4e0e\u4f20\u7edf DI \u7684\u5bf9\u6bd4<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>\u539f\u751f DI\uff08\u65e7\uff09<\/th><th>Keyed Services\uff08\u65b0\uff09<\/th><\/tr><\/thead><tbody><tr><td>\u591a\u4e2a\u5b9e\u73b0<\/td><td>\u4e0d\u652f\u6301<\/td><td>\u2714 \u652f\u6301<\/td><\/tr><tr><td>\u9009\u62e9\u7279\u5b9a\u5b9e\u73b0<\/td><td>\u9ebb\u70e6\u3001\u9700 IEnumerable + \u624b\u52a8\u7b5b\u9009<\/td><td>\u2714 \u539f\u751f\u6309\u952e\u9009\u62e9<\/td><\/tr><tr><td>\u53ef\u8bfb\u6027<\/td><td>\u5dee<\/td><td>\u2714 \u6e05\u6670<\/td><\/tr><tr><td>\u5de5\u5382\u6a21\u5f0f<\/td><td>\u9700\u8981\u624b\u5199<\/td><td>\u2714 \u539f\u751f\u652f\u6301<\/td><\/tr><tr><td>\u6846\u67b6\u539f\u751f\u652f\u6301<\/td><td>\u274c<\/td><td>\u2714 .NET 8 \u539f\u751f\u652f\u6301<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Keyed Services \u672c\u8d28\u4e0a\u4e3a\u539f\u751f DI \u5b8c\u6210\u4e86\u6700\u7f3a\u7684\u90a3\u4e00\u5757\u62fc\u56fe\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">5\ufe0f\u20e3 \u6700\u4f73\u5b9e\u8df5\uff08\u52a1\u5fc5\u6536\u85cf\uff09<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u907f\u514d\u4f7f\u7528\u5b57\u7b26\u4e32 key\uff0c\u63a8\u8350 enum \u6216\u5e38\u91cf<\/strong><\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic enum PayType { Wechat, Alipay, Paypal }\n\n<\/pre><\/div>\n\n\n<p>\u6ce8\u518c\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nbuilder.Services.AddKeyedTransient&amp;lt;IPayment, WechatPay&gt;(PayType.Wechat);\n\n<\/pre><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u5de5\u5382\u7edf\u4e00\u7ba1\u7406 keyed services\uff08\u66f4\u4f18\u96c5\uff09<\/strong><\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class PaymentFactory\n{\n    private readonly IServiceProvider _sp;\n    public PaymentFactory(IServiceProvider sp) =&gt; _sp = sp;\n\n    public IPayment Create(PayType type)\n        =&gt; _sp.GetKeyedService&amp;lt;IPayment&gt;(type)!;\n}\n\n<\/pre><\/div>\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>KeyedService + Options \u975e\u5e38\u5f3a\u5927<\/strong><\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nbuilder.Services.AddKeyedSingleton&amp;lt;IPayment&gt;(&quot;wechat&quot;, (sp, key) =&gt;\n{\n    var opt = sp.GetRequiredService&amp;lt;IOptions&amp;lt;WechatOptions&gt;&gt;().Value;\n    return new WechatPay(opt);\n});\n\n<\/pre><\/div>\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>\u4e0d\u53ef\u4e0e Named HttpClient \u6df7\u6dc6<\/strong><br>KeyedService \u662f\u901a\u7528\u7684\uff1bHttpClient \u6709\u4e13\u95e8\u7684\u547d\u540d HttpClient\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">6\ufe0f\u20e3 \u5b8c\u6574\u793a\u4f8b\uff08\u53ef\u76f4\u63a5\u590d\u5236\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\u6ce8\u518c<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nbuilder.Services.AddKeyedTransient&amp;lt;IPayment, WechatPay&gt;(&quot;wechat&quot;);\nbuilder.Services.AddKeyedTransient&amp;lt;IPayment, Alipay&gt;(&quot;alipay&quot;);\nbuilder.Services.AddKeyedTransient&amp;lt;IPayment, Paypal&gt;(&quot;paypal&quot;);\nbuilder.Services.AddSingleton&amp;lt;PaymentFactory&gt;();\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u670d\u52a1<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class PayController\n{\n    private readonly PaymentFactory _factory;\n\n    public PayController(PaymentFactory factory)\n    {\n        _factory = factory;\n    }\n\n    &#x5B;HttpGet(&quot;pay\/{channel}&quot;)]\n    public string Pay(string channel)\n    {\n        var payment = _factory.Create(channel);\n        return payment.Pay();\n    }\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u5de5\u5382<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class PaymentFactory\n{\n    private readonly IServiceProvider _sp;\n\n    public PaymentFactory(IServiceProvider sp)\n    {\n        _sp = sp;\n    }\n\n    public IPayment Create(string key)\n        =&gt; _sp.GetKeyedService&amp;lt;IPayment&gt;(key)!;\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\">\ud83d\udccc \u603b\u7ed3<\/h1>\n\n\n\n<p><strong>Keyed Services \u89e3\u51b3\u4e86 .NET DI \u6700\u5927\u7684\u75db\u70b9\u4e4b\u4e00\uff1a\u591a\u5b9e\u73b0\u9009\u62e9\u3002<\/strong><br>\u4f18\u52bf\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u539f\u751f\u652f\u6301\u591a\u5b9e\u73b0\u7684\u6ce8\u5165\u4e0e\u9009\u62e9<\/li>\n\n\n\n<li>\u4e0d\u9700\u624b\u5199\u5de5\u5382\uff08\u4e5f\u652f\u6301\u5de5\u5382\uff09<\/li>\n\n\n\n<li>\u66f4\u6613\u6269\u5c55<\/li>\n\n\n\n<li>\u66f4\u6613\u9605\u8bfb<\/li>\n\n\n\n<li>\u907f\u514d\u6ce8\u518c\u51b2\u7a81<\/li>\n<\/ul>\n\n\n\n<p>\u5b83\u4f1a\u6210\u4e3a\u4eca\u540e .NET \u5f00\u53d1\u4e2d\u7279\u522b\u5e38\u89c1\u7684 DI \u5199\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u5199\u4e00\u7bc7 \u901a\u4fd7\u6613\u61c2\u3001\u53ef\u76f4\u63a5\u7528\u4e8e\u535a\u5ba2\/\u6280\u672f\u6587\u6863\u7684\u300a.NET 8 \u4e2d\u7684 Key&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/net-8-%e4%b8%ad%e7%9a%84-keyed-services%e8%a7%a3%e9%94%81%e4%be%9d%e8%b5%96%e6%b3%a8%e5%85%a5%e7%9a%84%e6%96%b0%e6%96%b9%e5%bc%8f%e6%9c%80%e6%96%b0%e6%8e%a8%e8%8d%90\/\">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":[49],"tags":[],"class_list":["post-609","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/609","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=609"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/609\/revisions"}],"predecessor-version":[{"id":610,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/609\/revisions\/610"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}