問題
如何使用 ASP.NET Core 服務容器進行依賴注入?
答案
創建一個服務
public interface IGreetingService { string Greet(string to); } public class GreetingService : IGreetingService { public string Greet(string to) { return $"Hello {to}"; } }
然后可以在需要的時候注入,下面將此服務注入一個中間件(Middleware):
public class HelloWorldMiddleware { private readonly RequestDelegate _next; public HelloWorldMiddleware(RequestDelegate next) { _next = next; } public async Task Invoke(HttpContext context, IGreetingService greetingService) { var message = greetingService.Greet("World (via DI)"); await context.Response.WriteAsync(message); } }
使用此中間件的擴展方法(IApplicationBuilder):
public static class UseMiddlewareExtensions { public static IApplicationBuilder UseHelloWorld(this IApplicationBuilder app) { return app.UseMiddleware<HelloWorldMiddleware>(); } }
下面需要將此服務添加到ASP.NET Core的服務容器中,位于Startup.cs文件的ConfigureServices()方法:
public void ConfigureServices(IServiceCollection services) { services.AddScoped<IGreetingService, GreetingService>(); }
然后在請求管道中(request pipeline)使用此中間件,位于Startup.cs文件的Configure()方法:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseHelloWorld(); }
運行,此時頁面輸出:
創建一個帶輸入參數的服務
如果你的服務需要更復雜的初始化參數,下面我們創建一個FlexibleGreetingService:
public class FlexibleGreetingService : IGreetingService { private readonly string _sayWhat; public FlexibleGreetingService(string sayWhat) { _sayWhat = sayWhat; } public string Greet(string to) { return $"{_sayWhat} {to}"; } }
我們可以使用AddScoped的一個重載工廠方法來添加此服務到容器中:
public void ConfigureServices(IServiceCollection services) { services.AddScoped<IGreetingService, FlexibleGreetingService>(factory => { return new FlexibleGreetingService("Hi"); }); }
運行,此時頁面輸出:
如果是單件生命周期,還有一個接受服務實例的重載方法:
public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IGreetingService>(new FlexibleGreetingService("Hi ")); }
討論
ASP.NET Core內置了一個輕量級的服務容器。我們可以在Startup.cs類的ConfigureServices()方法中配置需要的服務。這個方法在Configure()方法之前執行,所以我們可以在任意中間件使用之前配置的服務(包含MVC服務)。
依賴注入默認是通過公開構造函數來完成的,大多數情況下這是最佳實踐。
服務的生命周期
服務容器管理著添加到服務器列表的生命周期。下面列出了添加服務的三種方法:
- AddScoped():服務會在一個請求內部只創建一次。
- AddTransient():服務會在每次需要時創建一次。
- AddSingleton():服務會在第一次需要時創建一次,并在隨后保持不變。
注:EF的生命周期應該是Scoped,我們可以通過IServiceCollection.AddDbContext來創建EF服務(內部也是作為Scoped實現)。
工廠方法
上面的方法都有一個重載方法來使用工廠方法來添加服務。對于需要復雜配置的服務這是很有用的。
這些方法的簽名看起來如下所示:
AddScoped(Func<IServiceProvider, TService>)
框架提供的服務
ConfigureServices()接受的IServiceCollection參數擁有很多內置的服務(由框架提供),可以參考ASP.NET Core文檔。
IServiceCollection有很多有用的擴展方法來添加常用服務,比如AddDbContext,AddIdentity,AddOptions和AddMvc。
銷毀服務
服務容器會自動調用所有實現了IDisposable接口的服務類型,除了那些作為實例(而不是類型)添加的服務。
獲取服務(Request Services)
盡管通過構造函數來注入服務被認為是最佳實踐,我們依然可以通過IServiceProvider的GetService方法來獲取服務。在中間件中IServiceProvider對象可以通過HttpContext來獲取:
public async Task Invoke(HttpContext context) { var greetingService = context.RequestServices.GetService<IGreetingService>(); var message = greetingService.Greet("World (via GetService)"); await context.Response.WriteAsync(message); }
注:需要添加Microsoft.Extensions.DependencyInjection引用才能上述使用GetService的泛型重載方法。
運行,此時頁面輸出:
源代碼下載
原文:https://tahirnaushad.com/2017/08/15/asp-net-core-dependency-injection/
文章列表