<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ASP.NET Core依賴注入容器中的動態(tài)服務(wù)注冊

          共 1633字,需瀏覽 4分鐘

           ·

          2022-03-18 14:30

          介紹

          ASP.NET Core中,每當(dāng)我們將服務(wù)作為依賴項注入時,都必須將此服務(wù)注冊到ASP.NET Core依賴項注入容器。但是,一個接一個地注冊服務(wù)不僅繁瑣且耗時,而且容易出錯。因此,在這里,我們將討論如何動態(tài)地一次注冊所有服務(wù)。

          讓我們開始吧!

          為了動態(tài)注冊所有服務(wù),我們將使用AspNetCore.ServiceRegistration.Dynamic?庫。這是一個很小但非常有用的庫,使您可以在不公開服務(wù)實現(xiàn)的情況下立即將所有服務(wù)注冊到ASP.NET Core依賴注入容器中。

          現(xiàn)在,首先將最新版本的AspNetCore.ServiceRegistration.Dynamic?nuget軟件包安裝到您的項目中,如下所示:

          Install-Package AspNetCore.ServiceRegistration.Dynamic

          現(xiàn)在,讓您的服務(wù)繼承任何ITransientService,IScoperServiceISingletonService標(biāo)記接口,如下所示:

          1. // Inherit `IScopedService` interface if you want to register `IEmployeeService`

          2. // as scoped service.

          3. public class IEmployeeService : IScopedService

          4. {

          5. Task CreateEmployeeAsync(Employee employee);

          6. }


          7. internal class EmployeeService : IEmployeeService

          8. {

          9. public async Task CreateEmployeeAsync(Employee employee)

          10. {

          11. // Implementation here

          12. };

          13. }

          現(xiàn)在在您ConfigureServicesStartup類方法中:

          1. public void ConfigureServices(IServiceCollection services)

          2. {

          3. services.RegisterAllTypes(); // This will register all the

          4. // Scoped services of your application.

          5. services.RegisterAllTypes(); // This will register all the

          6. // Transient services of your application.

          7. services.RegisterAllTypes(); // This will register all the

          8. // Singleton services of your application.


          9. services.AddControllersWithViews();

          10. }

          AspNetCore.ServiceRegistration.Dynamic.Extensions名稱空間中RegisterAllTypes是可用的。

          結(jié)論

          僅此而已!任務(wù)完成!像上面一樣簡單,可以一次將所有服務(wù)動態(tài)注冊到ASP.NET Core Dependency Injection容器中。如果有任何問題,可以將其提交到該Github存儲庫。您會盡快得到幫助。

          .NET/.NET Core Dynamic Service Registration:

          ?https://github.com/TanvirArjel/TanvirArjel.Extensions.Microsoft.DependencyInjection


          ?????

          瀏覽 31
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  www.久久草 | 天天干91 | 清纯唯美亚洲第一页麻豆豆花 | 亚洲天堂精品视频 | 在线播放视频一区 |