using System; using System.Linq; using Autofac.Extensions.DependencyInjection; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; namespace EntrustSettle.Extensions.Middlewares { /// /// 查看所有注入的服务 /// public static class AllServicesMiddleware { public static void UseAllServicesMiddle(this IApplicationBuilder app, IServiceCollection _services) { if (app == null) throw new ArgumentNullException(nameof(app)); //List tsDIAutofac = new List(); //tsDIAutofac.AddRange(Assembly.LoadFrom(Path.Combine(AppContext.BaseDirectory, "EntrustSettle.Services.dll")).GetTypes().ToList()); //tsDIAutofac.AddRange(Assembly.LoadFrom(Path.Combine(AppContext.BaseDirectory, "EntrustSettle.Repository.dll")).GetTypes().ToList()); var autofacContainers = (app.ApplicationServices.GetAutofacRoot())?.ComponentRegistry?.Registrations; app.Map("/allservices", builder => builder.Run(async context => { context.Response.ContentType = "text/html; charset=utf-8"; await context.Response.WriteAsync(""); await context.Response.WriteAsync($"

所有服务{_services.Count}个

"); foreach (var svc in _services) { await context.Response.WriteAsync(""); await context.Response.WriteAsync($""); await context.Response.WriteAsync($""); await context.Response.WriteAsync($""); await context.Response.WriteAsync(""); } foreach (var item in autofacContainers.ToList()) { var interfaceType = item.Services; foreach (var typeArray in interfaceType) { await context.Response.WriteAsync(""); await context.Response.WriteAsync($""); await context.Response.WriteAsync($""); await context.Response.WriteAsync($""); await context.Response.WriteAsync(""); } } await context.Response.WriteAsync("
类型生命周期Instance
{svc.ServiceType.FullName}{svc.Lifetime}{svc.ImplementationType?.Name}
{typeArray?.Description}{item.Lifetime}{item?.Target.Activator.ObjToString().Replace("(ReflectionActivator)", "")}
"); })); } } }