ASP.NET Core开发-获取全部注入(DI)服务

获取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

能够看到增长不少的服务。咱们也能够添加其余的,而后查看注入了哪些服务。

 

若是你以为本文对你有帮助,请点击“推荐”,谢谢。

相关文章
相关标签/搜索