NET Core开发-获取所有注入(DI)服务
2016-08-07 22:49
591 查看
NET Core开发-获取所有注入(DI)服务
获取ASP.NET Core中所有注入(DI)服务,在ASP.NET Core中加入了Dependency Injection依赖注入。我们在Controller,或者在ASP.NET Core程序中的其他地方使用注入的服务,如logging 等。
我们要怎样获取ASP.NET Core中所有注入(DI)服务呢,下面我们来一探究竟, 也可以来看看ASP.NET Core到底注入了哪些服务。
依赖注入简单介绍:
依赖注入(Dependency injection , DI)是一种实现对象及其合作者或依赖项之间松散耦合的技术。将类用来执行其操作的这些对象以某种方式提供给该类,而不是直接实例化合作者或使用静态引用。
我们新建一个ASP.NET Core 空应用程序。
然后Startup 中定义一个变量 private IServiceCollection _services;
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打印出来。
执行程序,可以看到,默认有14个服务。
然后我们也可以将这些服务在网页中展示出来。
我们在 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
可以看到增加很多的服务。我们也可以添加其他的,然后查看注入了哪些服务。
相关文章推荐
- ASP.NET Core开发-获取所有注入(DI)服务
- C#小技巧系列之二:获取系统所有服务信息
- WCF学习心得(1): 获取一个服务所有的契约中的操作
- iOS定位服务与地图开发(1)---获取经纬度
- 微信开发公共服务平台开发(.Net) - 网页授权获取用户基本信息
- Android开发之获取所有软件信息
- 如何获取局域网中的所有 SQL Server 服务
- AngularJS开发指南15:AngularJS的创建服务,将服务注入到控制器,管理服务依赖详解
- ios开发-获取系统相簿里边的所有照片
- Eclipse插件开发 获取Workspace下所有的项目
- 【Android开发经验】如何获取媒体库中所有音频文件信息,并在文件增删后及时更新媒体库
- 获取系统所有服务
- rcp(插件开发)如何获取显示在编辑区的所有编辑器及获取其中之一
- InstallShield高级应用--获取机机所有ORACLE服务列表
- Android开发之列出所有启动的服务以及判断某个服务是否启动
- C#小技巧系列之二:获取系统所有服务信息
- InstallShield高级应用--获取本机所有的SQL服务
- 根据WSDL的URL来获取服务中的所有方法
- (七)Android百度公交开发—百度地图API之城市POI搜索-获取所有结果
- 【Android开发经验】如何获取媒体库中所有音频文件信息,并在文件增删后及时更新媒体库