ASP.NET Core开发-获取所有注入(DI)服务
2016-08-05 10:59
846 查看
获取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;
在 Configure 中使用Logger打印出来。
执行程序,可以看到,默认有14个服务。
然后我们也可以将这些服务在网页中展示出来。
我们在 Configure 中加入如下代码:
然后我们使用自宿主的方式执行,在地址栏输入 http://localhost:5000/allservices
同样可以看到14个服务,这里我将/allservices 放在 Development 环境下,只有在程序调试时才能看到,运行是不行的。
我们在项目添加 Microsoft.AspNetCore.Mvc.Core 的引用 。
然后在 ConfigureServices 中加入
我们就可以看到MvcCore 里多加注入的服务
http://localhost:5000/allservices
可以看到增加很多的服务。我们也可以添加其他的,然后查看注入了哪些服务。
如果你觉得本文对你有帮助,请点击“推荐”,谢谢。
我们在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 手动获取注入对象
- NET Core开发-获取所有注入(DI)服务
- ASP.NET Core中的依赖注入(3): 服务的注册与提供
- ASP.NET Core中的依赖注入(2):依赖注入(DI)
- 【ASP.NET Core】依赖注入高级玩法——如何注入多个服务实现类
- Asp.Net Core 项目实战之权限管理系统(4) 依赖注入、仓储、服务的多项目分层实现
- 【无私分享:ASP.NET CORE 项目实战(第二章)】添加EF上下文对象,添加接口、实现类以及无处不在的依赖注入(DI)
- (7)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- 利用Polly+AOP+依赖注入封装的降级框架
- Asp.net core 学习笔记 ( DI 依赖注入 )
- 在ASP.NET Core中使用Apworks快速开发数据服务
- ASP.NET Core依赖注入多个服务实现类
- ASP.NET Core 2.0系列学习笔记-DI依赖注入
- 在ASP.NET Core中使用Apworks开发数据服务:对HAL的支持
- asp.net core 开发的https证书服务-agilelabs.net
- ASP.NET Core利用原生服务控制反转注入泛型类
- ASP.NET Core中的依赖注入(2):依赖注入(DI)
- 简易的开发框架(微服务) Asp.Net Core 2.0
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(六)-- 依赖注入
- 项目开发中的一些注意事项以及技巧总结 基于Repository模式设计项目架构—你可以参考的项目架构设计 Asp.Net Core中使用RSA加密 EF Core中的多对多映射如何实现? asp.net core下的如何给网站做安全设置 获取服务端https证书 Js异常捕获
- ASP.NET Core 注入和获取 AppSettings 配置