{"id":1027,"date":"2025-05-18T03:36:34","date_gmt":"2025-05-17T19:36:34","guid":{"rendered":"https:\/\/www.hyy.net\/?p=1027"},"modified":"2025-05-18T03:36:34","modified_gmt":"2025-05-17T19:36:34","slug":"csharp-dependency-injection","status":"publish","type":"post","link":"https:\/\/diji.net\/?p=1027","title":{"rendered":"\u7406\u89e3\u548c\u5165\u95e8\u4f9d\u8d56\u6ce8\u5165"},"content":{"rendered":"<p>\u7406\u89e3\u548c\u5165\u95e8\u4f9d\u8d56\u6ce8\u5165<\/p>\n<p>\u6982\u89c8\uff1a<\/p>\n<ol>\n<li>\n<p>\u4ec0\u4e48\u662f\u4f9d\u8d56\u6ce8\u5165\uff1f<\/p>\n<\/li>\n<li>\n<p>\u4f2a\u4ee3\u7801\u6f14\u793a<br \/>\n\u2022  \u4f9d\u8d56\u6ce8\u5165\u63d0\u5021\u4f7f\u7528\u63a5\u53e3\u800c\u4e0d\u662f\u7c7b<\/p>\n<\/li>\n<li>\n<p>C# \u4ee3\u7801\u6f14\u793a<br \/>\n\u2022  \u7248\u672c\u4e00\uff1a\u6700\u76f4\u63a5\u7c97\u66b4\u7684\u7248\u672c<br \/>\n\u2022  \u7248\u672c\u4e8c\uff1a\u968fHost\u81ea\u542f\u52a8\u7684\u670d\u52a1<\/p>\n<\/li>\n<li>\n<p>\u4f9d\u8d56\u7684\u5b9e\u4f8b\u5316\u7b56\u7565 \u2014\u2014 \u6bcf\u6b21\u90fd\u5b9e\u4f8b\u5316\/\u5355\u4f8b\/Scoped\u5b9e\u4f8b<br \/>\n\u2022  Scoped\u5b9e\u4f8b\u5316\u7b56\u7565\u7684C#\u4ee3\u7801\u6f14\u793a<\/p>\n<\/li>\n<\/ol>\n<h2>1. \u4ec0\u4e48\u662f\u4f9d\u8d56\u6ce8\u5165\uff1f<\/h2>\n<blockquote>\n<p>\u6765\u81ea\u767e\u79d1\u7684\u5b9a\u4e49<br \/>\n\u5728\u8f6f\u4ef6\u5de5\u7a0b\u4e2d\uff0c\u4f9d\u8d56\u6ce8\u5165\uff08dependency injection\uff0c\u7f29\u5199\u4e3a DI\uff09\u662f\u4e00\u79cd\u8f6f\u4ef6\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u4e5f\u662f\u5b9e\u73b0\u63a7\u5236\u53cd\u8f6c\u7684\u5176\u4e2d\u4e00\u79cd\u6280\u672f\u3002\u8fd9\u79cd\u6a21\u5f0f\u80fd\u8ba9\u4e00\u4e2a\u7269\u4ef6\u63a5\u6536\u5b83\u6240\u4f9d\u8d56\u7684\u5176\u4ed6\u7269\u4ef6\u3002\u201c\u4f9d\u8d56\u201d\u662f\u6307\u63a5\u6536\u65b9\u6240\u9700\u7684\u5bf9\u8c61\u3002\u201c\u6ce8\u5165\u201d\u662f\u6307\u5c06\u201c\u4f9d\u8d56\u201d\u4f20\u9012\u7ed9\u63a5\u6536\u65b9\u7684\u8fc7\u7a0b\u3002\u5728\u201c\u6ce8\u5165\u201d\u4e4b\u540e\uff0c\u63a5\u6536\u65b9\u624d\u4f1a\u8c03\u7528\u8be5\u201c\u4f9d\u8d56\u201d\u3002\u6b64\u6a21\u5f0f\u786e\u4fdd\u4e86\u4efb\u4f55\u60f3\u8981\u4f7f\u7528\u7ed9\u5b9a\u670d\u52a1\u7684\u5bf9\u8c61\u4e0d\u9700\u8981\u77e5\u9053\u5982\u4f55\u5efa\u7acb\u8fd9\u4e9b\u670d\u52a1\u3002\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\uff0c\u8fde\u63a5\u6536\u65b9\u5bf9\u8c61\u4e5f\u4e0d\u77e5\u9053\u5b83\u5b58\u5728\u7684\u5916\u90e8\u4ee3\u7801\u63d0\u4f9b\u63a5\u6536\u65b9\u6240\u9700\u7684\u670d\u52a1\u3002<\/p>\n<\/blockquote>\n<p>\u89e3\u6790\u4e00\u4e0b\uff1a<\/p>\n<ol>\n<li>\u4f9d\u8d56\u6ce8\u5165\u662f\u4e00\u79cd\u8f6f\u4ef6\u8bbe\u8ba1\u6a21\u5f0f<\/li>\n<li>\u65b9\u4fbf\u5b9e\u73b0\u63a7\u5236\u53cd\u8f6c\uff08\u7a0d\u540e\u89e3\u91ca\uff09<\/li>\n<li>\u901a\u5e38\u5728\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\u4e2d\u4f7f\u7528 \u2014\u2014 \u5982\u679c\u5bf9\u8c61A\u4ee3\u7801\u91cc\u8c03\u7528\u4e86\u5bf9\u8c61B\uff0c\u90a3\u4e48 B\u5c31\u662fA\u7684\u201c\u4f9d\u8d56\u201d\u3002<\/li>\n<li>\u201c\u6ce8\u5165\u201d\u6307\u7684\u662f\u628a\u5bf9\u8c61B\u4f20\u9012\u7ed9\u5bf9\u8c61A\u7684\u8fc7\u7a0b<\/li>\n<li>\u201c\u6ce8\u5165\u201d\u8fc7\u7a0b\u9700\u8981\u4e00\u5957\u201c\u5916\u90e8\u4ee3\u7801\u201d\u5e2e\u52a9\u5b9e\u73b0 \u2014\u2014 \u4e14\u79f0\u4e4b\u4e3a \u4f9d\u8d56\u6ce8\u5165\u7cfb\u7edf<\/li>\n<\/ol>\n<h2>2. \u4f2a\u4ee3\u7801\u793a\u4f8b<\/h2>\n<p>\u4e0d\u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165<\/p>\n<p>\u4e0d\u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165\uff0c\u5fc5\u987b\u5728\u4ee3\u7801\u7684\u67d0\u4e2a\u5730\u65b9\u4e3b\u52a8\u5730\u521b\u5efa\u6216\u8005\u83b7\u53d6\u5bf9\u8c61B\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class ClassA\n{\n    private readonly ClassB _classB;\n\n    public ClassA()\n    {\n        _classB = new ClassB(); \/\/\u4e3b\u52a8\u521b\u5efa\u5bf9\u8c61B\n    }\n\n    public void Process()\n    {\n        _classB.DoSomething();\n        \/\/\/ ...\n    }\n}\n\npublic class ClassB\n{\n    public void DoSomething()\n    {\n        \/\/\/ ...\n    }\n}\n<\/pre>\n<p>\u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165\uff08\u4ee5C#\u4e3a\u4f8b\uff09<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class ClassA\n{\n    private readonly ClassB _classB;\n\n    public ClassA(ClassB classB) \/\/ \u58f0\u660e\u6784\u9020\u5668\u91cc\u9700\u8981\u5bf9\u8c61B\u5f15\u7528\uff0c\u4f9d\u8d56\u6ce8\u5165\u6846\u67b6\u5c31\u4f1a\u81ea\u52a8\u6ce8\u5165\u5bf9\u8c61B\n    {\n        _classB = classB;\n    }\n\n    public void Process()\n    {\n        _classB.DoSomething();\n        \/\/ ...\n    }\n}\n\npublic class ClassB\n{\n    public void DoSomething()\n    {\n        \/\/ ...\n    }\n}\n\n\/\/ \u4f2a\u4ee3\u7801\uff1a \u5411\u4f9d\u8d56\u6ce8\u5165\u7cfb\u7edf\u4e2d\u6ce8\u518c ClassA \u548c ClassB\nDependencyInjectionSystem.AddType(ClassA);\nDependencyInjectionSystem.AddType(ClassB);&lt;\/code&gt;&lt;\/pre&gt;\n<\/pre>\n<p>\u2022  \u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165\uff0c\u4e0d\u9700\u8981\u5728\u4ee3\u7801\u91cc\u4e3b\u52a8\u5730\u521b\u5efa\u6216\u8005\u83b7\u53d6\u5bf9\u8c61B\uff0c\u76f8\u53cd\uff0c\u53ea\u9700\u8981\u5728\u6784\u9020\u5668\u53c2\u6570\u91cc\u58f0\u660e\u9700\u8981\u5bf9\u8c61B\u7684\u5f15\u7528\u3002\uff08\u5728\u5176\u4ed6\u4e00\u4e9b\u8bed\u8a00\u7684\u5177\u4f53\u5b9e\u73b0\u91cc\uff0c\u53ef\u80fd\u662f\u52a0\u6ce8\u89e3\u3001\u6216\u8005\u7279\u6b8a\u7684\u4fee\u9970\u7b26\u6765\u58f0\u660e\uff09\u3002<br \/>\n\u2022  \u4f2a\u4ee3\u7801\u4e2d\u865a\u6784\u4e86\u4e00\u4e2a DependencyInjectionSystem \u6307\u4ee3\u4f9d\u8d56\u6ce8\u5165\u7cfb\u7edf<br \/>\n\u2022  \u9700\u8981\u5411\u4f9d\u8d56\u6ce8\u5165\u7cfb\u7edf\u4e2d\u6ce8\u518c\u5bf9\u5e94\u7684\u7c7b<\/p>\n<p>\u6709\u8bfb\u8005\u53ef\u80fd\u4f1a\u95ee\uff1a\u201c\u90a3\u662f\u4e0d\u662f\u5728\u4e1a\u52a1\u4ee3\u7801\u4e2d\u9700\u8981\u5199 new ClassA(new ClassB()) \u6765\u5b9e\u4f8b\u5316ClassA\u5462\uff1f&quot;<\/p>\n<p>\u4e0d\u9700\u8981\u3002\u4f7f\u7528\u4e86\u4f9d\u8d56\u6ce8\u5165\u540e\uff0c\u4e1a\u52a1\u4ee3\u7801\u4e2d\u57fa\u672c\u5c31\u770b\u4e0d\u5230 new \u7684\u8e2a\u5f71\u4e86\uff0c\u4f60\u4f1a\u611f\u89c9\u4e0d\u7ba1\u662fClassA\u5bf9\u8c61\u8fd8\u662fClassB\u5bf9\u8c61\u90fd\u4eff\u4f5b\u662f\u51ed\u7a7a\u751f\u6210\u7684 \u2014\u2014 \u5176\u5b9e\u662f\u4f9d\u8d56\u6ce8\u5165\u7cfb\u7edf\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u81ea\u52a8\u751f\u6210\u7684\u3002<\/p>\n<p>\u63d0\u5021\u4f7f\u7528\u63a5\u53e3\u800c\u4e0d\u662f\u7c7b<\/p>\n<p>\u5728\u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165\u65f6\uff0c\u66f4\u591a\u65f6\u5019\uff0c\u5bf9\u4e8e\u201c\u4f9d\u8d56\u201d\uff0c\u63a5\u53e3\u662f\u66f4\u63a8\u8350\u7684\u3002\u6539\u8fdb\u4e0a\u9762\u7684\u4f8b\u5b50\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class ClassA\n{\n    private readonly IInterfaceB _b;\n\n    public ClassA(IInterfaceB b)\n    {\n        _b = b;\n    }\n\n    public void Process()\n    {\n        _b.DoSomething();\n        \/\/...\n    }\n}\n\npublic class ClassB : IInterfaceB\n{\n    public void DoSomething()\n    {\n        Console.WriteLine(&quot;class B is doing something ...&quot;);\n    }\n}\n\npublic interface IInterfaceB\n{\n    void DoSomething();\n}\n\n\/\/ \u4f2a\u4ee3\u7801\uff1a \u5411\u4f9d\u8d56\u6ce8\u5165\u7cfb\u7edf\u4e2d\u6ce8\u518c ClassA \u548c ClassB\nDependencyInjectionSystem.AddType(ClassA);\nDependencyInjectionSystem.AddType(IInterfaceB, ClassB);\n<\/pre>\n<p>\u2022  \u65b0\u589e\u4e86\u4e00\u4e2a\u63a5\u53e3 IInterfaceB<br \/>\n\u2022  ClassB \u5b9e\u73b0\u4e86\u8fd9\u4e2a\u63a5\u53e3<br \/>\n\u2022  ClassA \u91cc\u58f0\u660e\u5f15\u7528\u63a5\u53e3 IInterfaceB \u800c\u4e0d\u662f\u76f4\u63a5\u5f15\u7528\u7c7b ClassB<br \/>\n\u2022  \u6ce8\u518cClassB\u65f6\u9700\u8981\u6307\u660e\u5bf9\u5e94\u7684\u63a5\u53e3 IInterfaceB<\/p>\n<p>\u4f18\u70b9\uff1a\u89e3\u8026\u4e86\u63a5\u53e3\u548c\u5b9e\u73b0\uff0cClassA\u4e0d\u9700\u8981\u77e5\u9053\u6700\u7ec8\u5b83\u83b7\u5f97\u7684\u662f\u600e\u4e48\u6837\u7684\u5b9e\u73b0\uff0c\u5b83\u53ea\u9700\u8981\u77e5\u9053 IInterfaceB \u6709\u4e2a\u65b9\u6cd5\u53eb DoSomething() \u5e76\u4e14\u8c03\u7528\u5b83\u5c31\u884c\u4e86\u3002\u5f53\u524d\u5b83\u53ef\u80fd\u7528\u7684\u662f ClassB \u5b9e\u4f8b\uff0c\u4e3a\u4e86\u53ef\u80fd\u7528\u7684\u662f\u6539\u8fdb\u540e\u7684 ClassB2 \u5b9e\u4f8b\uff0c\u4e5f\u53ef\u80fd\u662f\u5168\u65b0\u7684 ClassX \u5b9e\u4f8b\uff0c\u4f46ClassA\u4e0d\u9700\u8981\u66f4\u6539\u4efb\u4f55\u4ee3\u7801\u3002<\/p>\n<p>\u539f\u672c\u662f\u5e94\u8be5 ClassA \u6765\u63a7\u5236\u4f7f\u7528\u600e\u6837\u7684 ClassB \u5b9e\u4f8b\uff08IInterfaceB \u5b9e\u73b0\uff09\uff0c\u4f7f\u7528\u4e86\u4f9d\u8d56\u6ce8\u5165\u8bbe\u8ba1\u540e\uff0c\u8fd9\u79cd\u63a7\u5236\u5173\u7cfb\uff08\u51b3\u5b9a\u5173\u7cfb\uff09\u5c31\u53cd\u8fc7\u6765\u7531\u5916\u90e8\u63a7\u5236\u4e86\uff0c\u8fd9\u5c31\u662f\u6240\u8c13\u7684\u201c\u63a7\u5236\u53cd\u8f6c\u201d\u3002<\/p>\n<p>\u540c\u65f6\u9f13\u52b1\u63a5\u53e3\u7684\u8bbe\u8ba1\u53ef\u4ee5\u5f15\u5bfc\u6211\u4eec\u5199\u51fa\u66f4\u7b26\u5408\u91cc\u6c0f\u4ee3\u6362\u539f\u5219\u7684\u4ee3\u7801 \uff08\u91cc\u6c0f\u4ee3\u6362\u539f\u5219_\u767e\u5ea6\u767e\u79d1 \uff09<br \/>\n\u91cc\u6c0f\u4ee3\u6362\u539f\u5219(Liskov Substitution Principle LSP)\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u7684\u57fa\u672c\u539f\u5219\u4e4b\u4e00\u3002 \u91cc\u6c0f\u4ee3\u6362\u539f\u5219\u4e2d\u8bf4\uff0c\u4efb\u4f55\u57fa\u7c7b\u53ef\u4ee5\u51fa\u73b0\u7684\u5730\u65b9\uff0c\u5b50\u7c7b\u4e00\u5b9a\u53ef\u4ee5\u51fa\u73b0\u3002 LSP\u662f\u7ee7\u627f\u590d\u7528\u7684\u57fa\u77f3\uff0c\u53ea\u6709\u5f53\u884d\u751f\u7c7b\u53ef\u4ee5\u66ff\u6362\u6389\u57fa\u7c7b\uff0c\u8f6f\u4ef6\u5355\u4f4d\u7684\u529f\u80fd\u4e0d\u53d7\u5230\u5f71\u54cd\u65f6\uff0c\u57fa\u7c7b\u624d\u80fd\u771f\u6b63\u88ab\u590d\u7528\uff0c\u800c\u884d\u751f\u7c7b\u4e5f\u80fd\u591f\u5728\u57fa\u7c7b\u7684\u57fa\u7840\u4e0a\u589e\u52a0\u65b0\u7684\u884c\u4e3a\u3002\u91cc\u6c0f\u4ee3\u6362\u539f\u5219\u662f\u5bf9\u201c\u5f00-\u95ed\u201d\u539f\u5219\u7684\u8865\u5145\u3002\u5b9e\u73b0\u201c\u5f00-\u95ed\u201d\u539f\u5219\u7684\u5173\u952e\u6b65\u9aa4\u5c31\u662f\u62bd\u8c61\u5316\u3002\u800c\u57fa\u7c7b\u4e0e\u5b50\u7c7b\u7684\u7ee7\u627f\u5173\u7cfb\u5c31\u662f\u62bd\u8c61\u5316\u7684\u5177\u4f53\u5b9e\u73b0\uff0c\u6240\u4ee5\u91cc\u6c0f\u4ee3\u6362\u539f\u5219\u662f\u5bf9\u5b9e\u73b0\u62bd\u8c61\u5316\u7684\u5177\u4f53\u6b65\u9aa4\u7684\u89c4\u8303\u3002<\/p>\n<h2>3. C# \u4ee3\u7801\u6f14\u793a<\/h2>\n<p>\u7406\u8bba\u5230\u6b64\u4e3a\u6b62\uff0c\u63a5\u4e0b\u6765\u770b\u770b\u5982\u4f55\u5199\u51fa\u80fd\u5b9e\u9645\u8fd0\u884c\u7684\u4ee3\u7801\uff08\u4ee5C#\u4e3a\u4f8b\uff09<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/  .csproj\n&lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;\n\n\t&lt;PropertyGroup&gt;\n\t\t&lt;OutputType&gt;Exe&lt;\/OutputType&gt;\n\t\t&lt;TargetFramework&gt;net6.0&lt;\/TargetFramework&gt;\n\t\t&lt;ImplicitUsings&gt;enable&lt;\/ImplicitUsings&gt;\n\t\t&lt;Nullable&gt;enable&lt;\/Nullable&gt;\n\t&lt;\/PropertyGroup&gt;\n\n\t&lt;ItemGroup&gt;\n\t\t&lt;PackageReference Include=&quot;Microsoft.Extensions.Hosting&quot; Version=&quot;6.0.1&quot; \/&gt;\n\t\t&lt;PackageReference Include=&quot;Microsoft.Extensions.Hosting.Abstractions&quot; Version=&quot;6.0.0&quot; \/&gt;\n\t&lt;\/ItemGroup&gt;\n\n&lt;\/Project&gt;\n<\/pre>\n<p>ClassA.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nnamespace ConsoleDIApp.demo\n{\n    public class ClassA\n    {\n        private readonly IInterfaceB _b;\n\n        public ClassA(IInterfaceB b)\n        {\n            _b = b;\n        }\n\n        public void Process()\n        {\n            Console.WriteLine(&quot;Class A start process&quot;);\n            _b.DoSomething();\n            Console.WriteLine(&quot;Class A finish process&quot;);\n        }\n    }\n}\n<\/pre>\n<p>ClassB.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nnamespace ConsoleDIApp.demo\n{\n    public class ClassB : IInterfaceB\n    {\n        public void DoSomething()\n        {\n            Console.WriteLine(&quot;class B is doing something ...&quot;);\n        }\n    }\n}\n<\/pre>\n<p>IInterfaceB.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nnamespace ConsoleDIApp.demo\n{\n    public interface IInterfaceB\n    {\n        void DoSomething();\n    }\n}\n<\/pre>\n<p>Program.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing ConsoleDIApp.demo;\nusing Microsoft.Extensions.DependencyInjection;\nusing Microsoft.Extensions.Hosting;\n\nnamespace ConsoleDIApp\n{\n    internal class Program\n    {\n        public static void Main(string&#x5B;] args)\n        {\n            IHost host = Host.CreateDefaultBuilder(args)\n                                   .ConfigureServices((context, services) =&gt;\n                                   {\n                                       services.AddSingleton&lt;ClassA&gt;();\n                                       services.AddSingleton&lt;IInterfaceB, ClassB&gt;();\n                                   })\n                                   .Build();\n\n            ClassA a = host.Services.GetRequiredService&lt;ClassA&gt;();\n            a.Process();\n        }\n    }\n}\n<\/pre>\n<p>\u5b9e\u9645\u8ddf\u4f9d\u8d56\u6ce8\u5165\u7cfb\u7edf\u6709\u5173\u7684\u4ee3\u7801\u53ea\u6709\u5927\u69827\u884c\u3002\u8fd0\u884c\u4ee5\u4e0a\u4ee3\u7801\uff0c\u5f97\u5230:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nClass A start process\nclass B is doing something ...\nClass A finish process\n<\/pre>\n<p>\u89e3\u6790\uff1a<\/p>\n<p>\u2022  Host \u4ee5\u53ca\u5b83\u5185\u90e8\u7684 Services \u53ef\u4ee5\u7b80\u5355\u7406\u89e3\u4e3a C# \u63d0\u4f9b\u7684 \u4f9d\u8d56\u6ce8\u5165\u7cfb\u7edf<br \/>\n\u2022  \u901a\u8fc7 GetRequiredService \u83b7\u5f97\u60f3\u8981\u7684\u5bf9\u5e94\u7684\u5b9e\u4f8b\uff08\u8fd9\u91cc\u662f ClassA \u5b9e\u4f8b\uff09<br \/>\n\u2022  \u8c03\u7528\u4e86ClassA\u5b9e\u4f8b a \u7684\u65b9\u6cd5 Process() \u540e\u7684\u8f93\u51fa\u7ed3\u679c\u53ef\u4ee5\u8bc1\u5b9e ClassB \u7684\u5b9e\u4f8b\u7684\u786e\u88ab\u81ea\u52a8\u751f\u6210\u4e86<\/p>\n<p>\u95ee\u9898\uff1a\u8fd9\u4efd\u7a0b\u5e8f\u7684\u903b\u8f91\u4e0a\u5165\u53e3\u5176\u5b9e\u5c31\u662f\u6267\u884c ClassA \u5b9e\u4f8b\u7684 Process() \u65b9\u6cd5\uff0c\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u91cc\u663e\u5f97\u5341\u5206\u751f\u786c\u3002<\/p>\n<p>\u6709\u6ca1\u6709\u66f4\u4f18\u96c5\u7684\u5199\u6cd5\u5462\uff1f \u2014\u2014 \u6709\u7684\uff01<\/p>\n<p>\u53ef\u8fd0\u884c\u7248\u672c\u4e8c<br \/>\n\u65b0\u589e\u7c7bHostedService.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing ConsoleDIApp.demo;\nusing Microsoft.Extensions.Hosting;\n\nnamespace ConsoleDIApp\n{\n    public class HostedService : IHostedService\n    {\n        private readonly ClassA _a;\n\n        public HostedService(ClassA a)\n        {\n            _a = a;\n        }\n\n        public Task StartAsync(CancellationToken cancellationToken)\n        {\n            _a.Process();\n            return Task.CompletedTask;\n        }\n\n        public Task StopAsync(CancellationToken cancellationToken)\n        {\n            return Task.CompletedTask;\n        }\n    }\n}\n<\/pre>\n<p>\u4fee\u6539 Program.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nnamespace ConsoleDIApp\n{\n    internal class Program\n    {\n        public static async Task Main(string&#x5B;] args)\n        {\n            IHost host = Host.CreateDefaultBuilder(args)\n                                   .ConfigureServices((context, services) =&gt;\n                                   {\n                                       services.AddSingleton&lt;ClassA&gt;();\n                                       services.AddSingleton&lt;IInterfaceB, ClassB&gt;();\n\n                                       services.AddHostedService&lt;HostedService&gt;();\n                                   })\n                                   .Build();\n\n            await host.RunAsync();\n        }\n    }\n}\n<\/pre>\n<p>\u8fd0\u884c\u7684\u7ed3\u679c\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p>\u89e3\u6790\uff1a<br \/>\n\u2022  IHostedService \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u63a5\u53e3\uff0c\u5b9e\u73b0\u8fd9\u4e2a\u63a5\u53e3\u7684\u7c7b\u5728\u901a\u8fc7\u5728 Host services \u91cc\u6ce8\u518c\u540e\uff0c\u53ef\u4ee5\u5728 Host \u8fd0\u884c\u65f6\u81ea\u52a8\u6267\u884c<br \/>\n\u2022  services.AddHostedService<HostedService>(); \u7528\u4e8e\u6ce8\u518c\u81ea\u52a8\u8fd0\u884c\u7684\u7c7b<br \/>\n\u2022  await host.RunAsync(); \u8fd0\u884c Host \u5b9e\u4f8b<\/p>\n<p>\u4e0a\u9762\u7684\u5168\u90e8\u4ee3\u7801<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing ConsoleDIApp.demo;\nusing Microsoft.Extensions.DependencyInjection;\nusing Microsoft.Extensions.Hosting;\n\nnamespace ConsoleDIApp\n{\n    internal class Program\n    {\n        public static async Task Main(string&#x5B;] args)\n        {\n            IHost host = Host.CreateDefaultBuilder(args)\n                                   .ConfigureServices((context, services) =&gt;\n                                   {\n                                       services.AddSingleton&lt;ClassA&gt;();\n                                       services.AddSingleton&lt;IInterfaceB, ClassB&gt;();\n\n                                       services.AddHostedService&lt;HostedService&gt;();\n                                   })\n                                   .Build();\n\n            await host.RunAsync();\n        }\n    }\n}\n\nnamespace ConsoleDIApp.demo\n{\n    public class ClassA\n    {\n        private readonly IInterfaceB _b;\n\n        public ClassA(IInterfaceB b)\n        {\n            _b = b;\n        }\n\n        public void Process()\n        {\n            Console.WriteLine(&quot;Class A start process&quot;);\n            _b.DoSomething();\n            Console.WriteLine(&quot;Class A finish process&quot;);\n        }\n    }\n\n}\n\nnamespace ConsoleDIApp.demo\n{\n    public class ClassB : IInterfaceB\n    {\n        public void DoSomething()\n        {\n            Console.WriteLine(&quot;class B is doing something ...&quot;);\n        }\n    }\n}\n\nnamespace ConsoleDIApp.demo\n{\n    public interface IInterfaceB\n    {\n        void DoSomething();\n    }\n}\n\nnamespace ConsoleDIApp\n{\n    public class HostedService : IHostedService\n    {\n        private readonly ClassA _a;\n\n        public HostedService(ClassA a)\n        {\n            _a = a;\n        }\n\n        public Task StartAsync(CancellationToken cancellationToken)\n        {\n            _a.Process();\n            return Task.CompletedTask;\n        }\n\n        public Task StopAsync(CancellationToken cancellationToken)\n        {\n            return Task.CompletedTask;\n        }\n    }\n}\n<\/pre>\n<h2>4. \u4f9d\u8d56\u7684\u5b9e\u4f8b\u5316\u7b56\u7565<\/h2>\n<p>\u4e0a\u9762\u6f14\u793a\u7684 ClassB \u592a\u7b80\u5355\u4e86\uff0c\u6ca1\u6709\u5185\u90e8\u72b6\u6001\u3002\u56e0\u6b64\u4e0d\u7ba1\u662f\u6bcf\u6b21\u6ce8\u5165\u65f6\u90fd\u65b0\u5efa\u4e00\u4e2a\u5b9e\u4f8b\u8fd8\u662f\u6bcf\u6b21\u6ce8\u5165\u540c\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5bf9\u7a0b\u5e8f\u90fd\u4e0d\u4f1a\u6709\u5f71\u54cd\u3002<\/p>\n<p>\u4f46\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u7684\u7c7b\uff0c \u6bd4\u5982 Config \uff08\u5b58\u653e\u4e86\u7a0b\u5e8f\u914d\u7f6e\u7684\u7c7b\uff09\uff0c UserPreference \uff08\u5b58\u653e\u4e86\u5f53\u524d\u7528\u6237\u7684\u504f\u597d\u8bbe\u7f6e\uff09\u5c31\u5f97\u8003\u8651\u5408\u9002\u7684\u5b9e\u4f8b\u5316\u7b56\u7565\u3002<\/p>\n<p>\u663e\u7136\u5bf9\u4e8e\u591a\u6570\u7a0b\u5e8f\uff0c Config \uff08\u5b58\u653e\u4e86\u7a0b\u5e8f\u914d\u7f6e\u7684\u7c7b\uff09\u53ea\u9700\u8981\u5b9e\u4f8b\u5316\u4e00\u6b21\u5c31\u884c\uff0c\u91cd\u590d\u5b9e\u4f8b\u5316\u53ea\u4f1a\u6d6a\u8d39\u8d44\u6e90\uff1b\u800cUserPreference \uff08\u5b58\u653e\u4e86\u5f53\u524d\u7528\u6237\u7684\u504f\u597d\u8bbe\u7f6e\uff09\uff0c\u65e2\u4e0d\u662f\u6bcf\u6b21\u90fd\u5b9e\u4f8b\u5316\u4e5f\u4e0d\u662f\u53ea\u5b9e\u4f8b\u5316\u4e00\u6b21\uff0c\u800c\u662f\u5e94\u8be5\u9488\u5bf9\u6bcf\u4e2a\u7528\u6237\u5b9e\u4f8b\u5316\u4e00\u6b21\uff0c\u540c\u4e00\u4e2a\u7528\u6237\u7684\u6240\u6709\u6d3b\u52a8\u90fd\u5e94\u8be5\u5bf9\u5e94\u540c\u4e00\u4e2a UserPreference \u5b9e\u4f8b\u3002<\/p>\n<p>\u4f9d\u8d56\u7684\u5b9e\u4f8b\u5316\u7b56\u7565\u53ef\u4ee5\u5206\u6210\u4e09\u79cd\u7c7b\u578b\uff1a<\/p>\n<p>\u6bcf\u6b21\u6ce8\u5165\u524d\u90fd\u751f\u6210\u4e00\u4e2a\u5168\u65b0\u7684\u5b9e\u4f8b<br \/>\n\u5bf9\u4e8e\u4e00\u4e2a\u7c7b\u53ea\u751f\u6210\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u6bcf\u6b21\u6ce8\u5165\u540c\u4e00\u4e2a\u5b9e\u4f8b<br \/>\n\u9488\u5bf9\u540c\u4e00scope\uff08\u8303\u7574\uff09\u53ea\u751f\u6210\u4e00\u4e2a\u5b9e\u4f8b \u2014\u2014 scope \u9700\u8981\u81ea\u5b9a\u4e49\uff0c\u6bd4\u5982\u4e00\u6b21 http request \u6216\u8005\u540c\u4e00\u7528\u6237\u7684\u6240\u6709\u8bbf\u95ee \u7b49\u7b49\u3002<\/p>\n<p>\u5bf9\u6b64\uff0cC# \u6709\u5bf9\u5e94\u7684API<\/p>\n<p>\u5206\u522b\u662f<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nservices.AddTransient&lt;IInterfaceB, ClassB&gt;(); \/\/ \u6bcf\u6b21\u90fd\u751f\u6210\u4e00\u4e2a\u5b9e\u4f8b\nservices.AddSingleton&lt;IInterfaceB, ClassB&gt;(); \/\/ \u5355\u4f8b\nservices.AddScoped&lt;IInterfaceB, ClassB&gt;(); \/\/ \u6bcf\u4e2a scope \u4e00\u4e2a\u5b9e\u4f8b\n<\/pre>\n<p>\uff08services \u7c7b\u578b\u662f Microsoft.Extensions.DependencyInjection.IServiceCollection\uff09<\/p>\n<p>\u5982\u4f55\u83b7\u53d6 Scoped \u5b9e\u4f8b<br \/>\n\u2022  IServiceProvider.CreateScope() \u65b9\u6cd5\u80fd\u521b\u5efaIServiceScope\u5b9e\u4f8b\u3002<br \/>\n\u2022  serviceScope.ServiceProvider \u57df\u4e5f\u662f\u4e00\u4e2aIServiceProvider\u5b9e\u4f8b\u3002<br \/>\n\u2022  \u83b7\u53d6 Scoped \u5b9e\u4f8b\u53ea\u80fd\u901a\u8fc7\u201c\u53ef\u8fd0\u884c\u7248\u672c\u4e00\u201d\u4e2d\u4e3b\u52a8\u8c03\u7528IServiceProvider.GetRequiredService()\u7684\u65b9\u5f0f\uff1b<br \/>\n\u2022  \u83b7\u53d6\u5b9e\u4f8b\u901a\u5e38\u662f\u4e00\u4e2a\u94fe\u8def\u8fc7\u7a0b\uff0c\u94fe\u8def\u4e0a\u7684\u4f9d\u8d56\u5982\u679c\u662fScoped \u5b9e\u4f8b\u5c31\u8fd4\u56de\u8be5Scoped\u552f\u4e00\u7684\u5b9e\u4f8b\uff0c\u5982\u679c\u4e0d\u662f\uff08 AddTransient \u6216 AddSingleton\uff09\u5c31\u6309\u5bf9\u5e94\u7684\u7b56\u7565\u5b9e\u4f8b\u5316\u5bf9\u5e94\u7684\u4f9d\u8d56\u3002<\/p>\n<p>\u7ed3\u5408\u8fd9\u4e09\u70b9\uff0c\u4ee5\u4e0b\u4e3a\u6838\u5fc3\u4ee3\u7801<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nIServiceProvider serviceProvider = host.Services;\nIServiceScope serviceScope = serviceProvider.CreateScope();\nIServiceProvider scopedServiceProvider = serviceScope.ServiceProvider;\nClassA a = scopedServiceProvider.GetRequiredService&lt;ClassA&gt;();\n<\/pre>\n<p>\u89c9\u5f97\u8ff7\u7cca\u4e0d\u8981\u7d27\uff0c\u5c1d\u8bd5\u8fd0\u884c\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\uff0c\u6478\u7d22\u4e00\u4e0b\u5176\u4e2d\u7684\u539f\u7406<br \/>\nClassA.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nnamespace ConsoleDIApp.demo\n{\n    public class ClassA\n    {\n        private readonly IInterfaceB _b;\n        private readonly long _id;\n\n        public ClassA(IInterfaceB b)\n        {\n            _b = b;\n            _id = DateTime.UtcNow.Ticks; \/\/ \u5229\u7528 id \u6765\u533a\u5206\u662f\u5426\u662f\u540c\u4e00\u4e2a\u5b9e\u4f8b\n        }\n\n        public void Process()\n        {\n            Console.WriteLine($&quot;&#x5B;{_id}]: Class A start process&quot;);\n            _b.DoSomething();\n            Console.WriteLine(&quot;Class A finish process&quot;);\n        }\n    }\n}\n<\/pre>\n<p>ClassB.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nnamespace ConsoleDIApp.demo\n{\n    public class ClassB : IInterfaceB\n    {\n        private readonly long _id;\n\n        public ClassB()\n        {\n            _id = DateTime.UtcNow.Ticks; \/\/ \u5229\u7528 id \u6765\u533a\u5206\u662f\u5426\u662f\u540c\u4e00\u4e2a\u5b9e\u4f8b\n        }\n\n        public void DoSomething()\n        {\n            Console.WriteLine($&quot;&#x5B;{_id}]: class B is doing something ...&quot;);\n        }\n    }\n}\n<\/pre>\n<p>IInterfaceB.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nnamespace ConsoleDIApp.demo\n{\n    public interface IInterfaceB\n    {\n        void DoSomething();\n    }\n}\n<\/pre>\n<p>HostedService.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing ConsoleDIApp.demo;\nusing Microsoft.Extensions.DependencyInjection;\nusing Microsoft.Extensions.Hosting;\n\nnamespace ConsoleDIApp\n{\n    public class HostedService : IHostedService\n    {\n        private readonly IServiceProvider _services;\n\n        \/\/ \u5173\u8054\u7528\u6237\u540d\u548cIServiceScope\u5b9e\u4f8b\u7684\u96c6\u5408\n        private Dictionary&lt;string, IServiceScope&gt; _scopes = new();\n\n        public HostedService(IServiceProvider a)\n        {\n            _services = a;\n        }\n\n        public Task StartAsync(CancellationToken cancellationToken)\n        {\n            while (true)\n            {\n                Console.WriteLine(&quot;Input User Name: &quot;);\n                var user = Console.ReadLine();\n                if (user == null || user == &quot;q&quot;)\n                {\n                    break; \/\/ \u9000\u51fa\u5faa\u73af\n                }\n                if (!_scopes.ContainsKey(user))\n                {\n                    \/\/ \u5bf9\u65b0\u7528\u6237\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 IServiceScope \u5b9e\u4f8b\n                    IServiceScope newServiceScope = _services.CreateScope();\n                    _scopes.Add(user, newServiceScope);\n                }\n\n                IServiceScope serviceScope = _scopes&#x5B;user];\n                IServiceProvider serviceProvider = serviceScope.ServiceProvider;\n                ClassA a = serviceProvider.GetRequiredService&lt;ClassA&gt;();\n                a.Process();\n            }\n            return Task.CompletedTask;\n        }\n\n        public Task StopAsync(CancellationToken cancellationToken)\n        {\n            return Task.CompletedTask;\n        }\n    }\n}\n<\/pre>\n<p>Program.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n        public static async Task Main(string&#x5B;] args)\n        {\n            IHost host = Host.CreateDefaultBuilder(args)\n                                   .ConfigureServices((context, services) =&gt;\n                                   {\n                                       \/\/ \u6bcf\u6b64\u5b9e\u4f8b\u4e00\u4e2a\u65b0\u7684ClassA\n                                       services.AddTransient&lt;ClassA&gt;();\n\n                                       \/\/ \u76f8\u540cscope\u5171\u4eab\u540c\u4e00\u4e2aClassB\u5b9e\u4f8b\n                                       services.AddScoped&lt;IInterfaceB, ClassB&gt;();\n                                       \n                                       services.AddHostedService&lt;HostedService&gt;();\n                                   })\n                                   .Build();\n\n\n\n            await host.RunAsync();\n        }\n<\/pre>\n<p>\u8fd0\u884c\u6548\u679c<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nInput User Name:\ntom\n&#x5B;638831071897299805]: Class A start process\n&#x5B;638831071897292850]: class B is doing something ...\nClass A finish process\nInput User Name:\njerry\n&#x5B;638831071933082335]: Class A start process\n&#x5B;638831071933082295]: class B is doing something ...\nClass A finish process\nInput User Name:\n<\/pre>\n<p>\u89c2\u5bdf\u70b9\uff1a<\/p>\n<p>\u2022  \u6bcf\u6b21 ClassA \u7684 id \u90fd\u4e0d\u540c \uff08\u56e0\u4e3a AddTransient \uff09<br \/>\n\u2022  tom \u7684 ClassB \u7684 id \u90fd\u4e00\u6837 \uff0cjerry \u7684 ClassB \u7684 id \u90fd\u4e00\u6837\uff0c \u8bf4\u660e AddScoped \u5982\u9884\u671f\u4e00\u6837\u8868\u73b0<\/p>\n<p>\u540e\u9762\u7684\u5168\u90e8\u4ee3\u7801<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing ConsoleDIApp;\nusing ConsoleDIApp.demo;\nusing Microsoft.Extensions.DependencyInjection;\nusing Microsoft.Extensions.Hosting;\n\nnamespace ConsoleDIApp.demo\n{\n    internal class Program\n    {\n        public static async Task Main(string&#x5B;] args)\n        {\n            IHost host = Host.CreateDefaultBuilder(args)\n                                   .ConfigureServices((context, services) =&gt;\n                                   {\n                                       \/\/ \u6bcf\u6b64\u5b9e\u4f8b\u4e00\u4e2a\u65b0\u7684ClassA\n                                       services.AddTransient&lt;ClassA&gt;();\n\n                                       \/\/ \u76f8\u540cscope\u5171\u4eab\u540c\u4e00\u4e2aClassB\u5b9e\u4f8b\n                                       services.AddScoped&lt;IInterfaceB, ClassB&gt;();\n\n                                       services.AddHostedService&lt;HostedService&gt;();\n                                   })\n                                   .Build();\n\n\n\n            await host.RunAsync();\n        }\n    }\n}\n\nnamespace ConsoleDIApp.demo\n{\n    public class ClassA\n    {\n        private readonly IInterfaceB _b;\n        private readonly long _id;\n\n        public ClassA(IInterfaceB b)\n        {\n            _b = b;\n            _id = DateTime.UtcNow.Ticks; \/\/ \u5229\u7528 id \u6765\u533a\u5206\u662f\u5426\u662f\u540c\u4e00\u4e2a\u5b9e\u4f8b\n        }\n\n        public void Process()\n        {\n            Console.WriteLine($&quot;&#x5B;{_id}]: Class A start process&quot;);\n            _b.DoSomething();\n            Console.WriteLine(&quot;Class A finish process&quot;);\n        }\n    }\n}\n\nnamespace ConsoleDIApp.demo\n{\n    public class ClassB : IInterfaceB\n    {\n        private readonly long _id;\n\n        public ClassB()\n        {\n            _id = DateTime.UtcNow.Ticks; \/\/ \u5229\u7528 id \u6765\u533a\u5206\u662f\u5426\u662f\u540c\u4e00\u4e2a\u5b9e\u4f8b\n        }\n\n        public void DoSomething()\n        {\n            Console.WriteLine($&quot;&#x5B;{_id}]: class B is doing something ...&quot;);\n        }\n    }\n}\n\nnamespace ConsoleDIApp.demo\n{\n    public interface IInterfaceB\n    {\n        void DoSomething();\n    }\n}\n\nnamespace ConsoleDIApp\n{\n    public class HostedService : IHostedService\n    {\n        private readonly IServiceProvider _services;\n\n        \/\/ \u5173\u8054\u7528\u6237\u540d\u548cIServiceScope\u5b9e\u4f8b\u7684\u96c6\u5408\n        private Dictionary&lt;string, IServiceScope&gt; _scopes = new();\n\n        public HostedService(IServiceProvider a)\n        {\n            _services = a;\n        }\n\n        public Task StartAsync(CancellationToken cancellationToken)\n        {\n            while (true)\n            {\n                Console.WriteLine(&quot;Input User Name: &quot;);\n                var user = Console.ReadLine();\n                if (user == null || user == &quot;q&quot;)\n                {\n                    break; \/\/ \u9000\u51fa\u5faa\u73af\n                }\n                if (!_scopes.ContainsKey(user))\n                {\n                    \/\/ \u5bf9\u65b0\u7528\u6237\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 IServiceScope \u5b9e\u4f8b\n                    IServiceScope newServiceScope = _services.CreateScope();\n                    _scopes.Add(user, newServiceScope);\n                }\n\n                IServiceScope serviceScope = _scopes&#x5B;user];\n                IServiceProvider serviceProvider = serviceScope.ServiceProvider;\n                ClassA a = serviceProvider.GetRequiredService&lt;ClassA&gt;();\n                a.Process();\n            }\n            return Task.CompletedTask;\n        }\n\n        public Task StopAsync(CancellationToken cancellationToken)\n        {\n            return Task.CompletedTask;\n        }\n    }\n}\n<\/pre>\n<p>\u672c\u6587\u5bf9 C# \u7684API\u90fd\u53ea\u662f\u7b80\u5355\u4ecb\u7ecd\uff0c.Net \u5f00\u53d1\u8005\u53ef\u4ee5\u81ea\u884c\u9605\u8bfb\u5b98\u65b9\u6587\u6863\uff0c\u8fdb\u4e00\u6b65\u7406\u89e3\u5404\u4e2a\u76f8\u5173\u7684\u7c7b\uff0c\u65b9\u6cd5\u7b49\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u53ea\u8981\u8f6f\u4ef6\u4e2d\u4f7f\u7528\u4e86\u5927\u91cf\u7684\u9762\u5411\u5bf9\u8c61\u5199\u6cd5\uff0c\u4f9d\u8d56\u6ce8\u5165\u5f80\u5f80\u662f\u907f\u4e0d\u5f00\u7684\u4e00\u4e2a\u6280\u672f\u3002<br \/>\n\u5177\u4f53\u7684API\u53ef\u80fd\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u662f\u80cc\u540e\u7684\u601d\u60f3\u548c\u8bbe\u8ba1\u7406\u5ff5\u5927\u540c\u5c0f\u5f02\u3002<br \/>\n\u5e0c\u671b\u672c\u6587\u80fd\u5bf9\u8bfb\u8005\u6709\u542f\u53d1\uff01<\/p>\n<p><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/592698341\">https:\/\/zhuanlan.zhihu.com\/p\/592698341<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7406\u89e3\u548c\u5165\u95e8\u4f9d\u8d56\u6ce8\u5165 \u6982\u89c8\uff1a \u4ec0\u4e48\u662f\u4f9d\u8d56\u6ce8\u5165\uff1f \u4f2a\u4ee3\u7801\u6f14\u793a \u2022 \u4f9d\u8d56\u6ce8\u5165\u63d0\u5021\u4f7f\u7528\u63a5\u53e3\u800c\u4e0d\u662f\u7c7b C# \u4ee3\u7801\u6f14\u793a \u2022 \u7248\u672c\u4e00\uff1a\u6700\u76f4\u63a5\u7c97\u66b4\u7684\u7248\u672c \u2022 \u7248\u672c\u4e8c\uff1a\u968fHost\u81ea\u542f\u52a8\u7684\u670d\u52a1 \u4f9d\u8d56\u7684\u5b9e\u4f8b\u5316\u7b56\u7565 \u2014\u2014 \u6bcf\u6b21\u90fd\u5b9e\u4f8b\u5316\/\u5355\u4f8b\/Scoped\u5b9e\u4f8b \u2022 Scoped\u5b9e\u4f8b\u5316\u7b56\u7565\u7684C#\u4ee3\u7801\u6f14\u793a 1. \u4ec0\u4e48\u662f\u4f9d\u8d56\u6ce8\u5165\uff1f \u6765\u81ea\u767e\u79d1\u7684\u5b9a\u4e49 \u5728\u8f6f\u4ef6\u5de5\u7a0b\u4e2d\uff0c\u4f9d\u8d56\u6ce8\u5165\uff08dependency injection\uff0c\u7f29\u5199\u4e3a DI\uff09\u662f\u4e00\u79cd\u8f6f\u4ef6\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u4e5f\u662f\u5b9e\u73b0\u63a7\u5236\u53cd\u8f6c\u7684\u5176\u4e2d\u4e00\u79cd\u6280\u672f\u3002\u8fd9\u79cd\u6a21\u5f0f\u80fd\u8ba9\u4e00\u4e2a\u7269\u4ef6\u63a5\u6536\u5b83\u6240\u4f9d\u8d56\u7684\u5176\u4ed6\u7269\u4ef6\u3002\u201c\u4f9d\u8d56\u201d\u662f\u6307\u63a5\u6536\u65b9\u6240\u9700\u7684\u5bf9\u8c61\u3002\u201c\u6ce8\u5165\u201d\u662f\u6307\u5c06\u201c\u4f9d\u8d56\u201d\u4f20\u9012\u7ed9\u63a5\u6536\u65b9\u7684\u8fc7\u7a0b\u3002\u5728\u201c\u6ce8\u5165\u201d\u4e4b\u540e\uff0c\u63a5\u6536\u65b9\u624d\u4f1a\u8c03\u7528\u8be5\u201c\u4f9d\u8d56\u201d\u3002\u6b64\u6a21\u5f0f\u786e\u4fdd\u4e86\u4efb\u4f55\u60f3\u8981\u4f7f\u7528\u7ed9\u5b9a\u670d\u52a1\u7684\u5bf9\u8c61\u4e0d\u9700\u8981\u77e5\u9053\u5982\u4f55\u5efa\u7acb\u8fd9\u4e9b\u670d\u52a1\u3002\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\uff0c\u8fde\u63a5\u6536\u65b9\u5bf9\u8c61\u4e5f\u4e0d\u77e5\u9053\u5b83\u5b58\u5728\u7684\u5916\u90e8\u4ee3\u7801\u63d0\u4f9b\u63a5\u6536\u65b9\u6240\u9700\u7684\u670d\u52a1\u3002 \u89e3\u6790\u4e00\u4e0b\uff1a \u4f9d\u8d56\u6ce8\u5165\u662f\u4e00\u79cd\u8f6f\u4ef6\u8bbe\u8ba1\u6a21\u5f0f \u65b9\u4fbf\u5b9e\u73b0\u63a7\u5236\u53cd\u8f6c\uff08\u7a0d\u540e\u89e3\u91ca\uff09 \u901a\u5e38\u5728\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\u4e2d\u4f7f\u7528 \u2014\u2014 \u5982\u679c\u5bf9\u8c61A\u4ee3\u7801\u91cc\u8c03\u7528\u4e86\u5bf9\u8c61B\uff0c\u90a3\u4e48 B\u5c31\u662fA\u7684\u201c\u4f9d\u8d56\u201d\u3002 \u201c\u6ce8\u5165\u201d\u6307\u7684\u662f\u628a\u5bf9\u8c61B\u4f20\u9012\u7ed9\u5bf9\u8c61A\u7684\u8fc7\u7a0b \u201c\u6ce8\u5165\u201d\u8fc7\u7a0b\u9700\u8981\u4e00\u5957\u201c\u5916\u90e8\u4ee3\u7801\u201d\u5e2e\u52a9\u5b9e\u73b0 \u2014\u2014 \u4e14\u79f0\u4e4b\u4e3a \u4f9d\u8d56\u6ce8\u5165\u7cfb\u7edf 2. \u4f2a\u4ee3\u7801\u793a\u4f8b \u4e0d\u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165 \u4e0d\u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165\uff0c\u5fc5\u987b\u5728\u4ee3\u7801\u7684\u67d0\u4e2a\u5730\u65b9\u4e3b\u52a8\u5730\u521b\u5efa\u6216\u8005\u83b7\u53d6\u5bf9\u8c61B\u3002 public class ClassA { private readonly ClassB _classB; public ClassA() { _classB = new ClassB(); \/\/\u4e3b\u52a8\u521b\u5efa\u5bf9\u8c61B [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1027","post","type-post","status-publish","format-standard","hentry","category-csharp"],"_links":{"self":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/1027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1027"}],"version-history":[{"count":0,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/1027\/revisions"}],"wp:attachment":[{"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}