Last active
August 25, 2016 08:53
-
-
Save RavenZZ/2f8478db362cd2d3a39e178380891a88 to your computer and use it in GitHub Desktop.
ASP.NET Core-获取所有注入(DI)服务
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!"); | |
| }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!"); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment