您的位置:首页 > 编程语言 > ASP

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;

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


可以看到增加很多的服务。我们也可以添加其他的,然后查看注入了哪些服务。

如果你觉得本文对你有帮助,请点击“推荐”,谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐