獲取ASP.NET Core中全部注入(DI)服務,在ASP.NET Core中加入了Dependency Injection依賴注入。html
咱們在Controller,或者在ASP.NET Core程序中的其餘地方使用注入的服務,如logging 等。git
咱們要怎樣獲取ASP.NET Core中全部注入(DI)服務呢,下面咱們來一探究竟, 也能夠來看看ASP.NET Core到底注入了哪些服務。github
依賴注入簡單介紹:app
依賴注入(Dependency injection , DI)是一種實現對象及其合做者或依賴項之間鬆散耦合的技術。將類用來執行其操做的這些對象以某種方式提供給該類,而不是直接實例化合做者或使用靜態引用。asp.net
咱們新建一個ASP.NET Core 空應用程序。async
而後Startup 中定義一個變量 private IServiceCollection _services;post
public class Startup { private IServiceCollection _services; // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { _services = services; } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(); var _logger = loggerFactory.CreateLogger("Services"); _logger.LogInformation($"Total Services Registered: {_services.Count}"); foreach (var service in _services) { _logger.LogInformation($"Service: {service.ServiceType.FullName}\n Lifetime: {service.Lifetime}\n Instance: {service.ImplementationType?.FullName}"); } if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); } }
在 Configure 中使用Logger打印出來。ui
執行程序,能夠看到,默認有14個服務。this
而後咱們也能夠將這些服務在網頁中展現出來。spa
咱們在 Configure 中加入以下代碼:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(); var _logger = loggerFactory.CreateLogger("Services"); _logger.LogInformation($"Total Services Registered: {_services.Count}"); foreach (var service in _services) { _logger.LogInformation($"Service: {service.ServiceType.FullName}\n Lifetime: {service.Lifetime}\n Instance: {service.ImplementationType?.FullName}"); } if (env.IsDevelopment()) { app.Map("/allservices", builder => builder.Run(async context => { context.Response.ContentType = "text/html; charset=utf-8"; await context.Response.WriteAsync($"<h1>全部服務{_services.Count}個</h1><table><thead><tr><th>類型</th><th>生命週期</th><th>Instance</th></tr></thead><tbody>"); foreach (var svc in _services) { await context.Response.WriteAsync("<tr>"); await context.Response.WriteAsync($"<td>{svc.ServiceType.FullName}</td>"); await context.Response.WriteAsync($"<td>{svc.Lifetime}</td>"); await context.Response.WriteAsync($"<td>{svc.ImplementationType?.FullName}</td>"); await context.Response.WriteAsync("</tr>"); } await context.Response.WriteAsync("</tbody></table>"); })); app.UseDeveloperExceptionPage(); } app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); }
而後咱們使用自宿主的方式執行,在地址欄輸入 http://localhost:5000/allservices
一樣能夠看到14個服務,這裏我將/allservices 放在 Development 環境下,只有在程序調試時才能看到,運行是不行的。
咱們在項目添加 Microsoft.AspNetCore.Mvc.Core 的引用 。
而後在 ConfigureServices 中加入
public void ConfigureServices(IServiceCollection services) { services.AddMvcCore();//添加MvcCore _services = services; }
咱們就能夠看到MvcCore 裏多加註入的服務
http://localhost:5000/allservices
能夠看到增長不少的服務。咱們也能夠添加其餘的,而後查看注入了哪些服務。
若是你以爲本文對你有幫助,請點擊「推薦」,謝謝。