Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Some Code to Visualize your ASP.NET Core Dependencies
IServiceCollection _services;
public void ConfigureServices(IServiceCollection services)
{
_services = services;
// rest omitted
}
public void Configure(IApplicationBuilder app)//, IWebHostEnvironment env)
{
// omitted code
app.Map("/allservices", builder => builder.Run(async context =>
{
var sb = new StringBuilder();
var dependencies = new Dictionary<string,string>();
sb.AppendLine("<pre>");
sb.AppendLine("digraph Services {");
var services = _services.Select(svc => svc.ServiceType.ToString()).ToHashSet();
foreach (var svc in _services)
{
var serviceName = svc.ServiceType.ToString();
var implementationName = svc.ImplementationType?.ToString();
if (implementationName != null)
{
var implDependencies = svc.ImplementationType.GetConstructors().SelectMany(cons => cons.GetParameters()).Select(p => p.ParameterType.ToString()).Distinct().Where(x => services.Contains(x));
if (implDependencies.Count() > 0)
{
// Register Constructor dependendencies
foreach(var d in implDependencies)
{
dependencies.TryAdd(implementationName, d);
}
}
}
}
Action<string, string, string, IEnumerable<string>> printGroup = (label, cluster, color, group) =>
{
if (group.Count() > 0)
{
sb.AppendLine($" subgraph cluster_{cluster} {{");
sb.AppendLine(" style = filled;");
sb.AppendLine($" color = {color};");
sb.AppendLine(" node[style = filled, color = white];");
sb.AppendLine($" label = \"{label}\";");
foreach (var item in group)
{
sb.AppendLine($" \"{item}\"");
}
sb.AppendLine(" }");
}
};
var scopedGroup = _services.Where(s => s.Lifetime == ServiceLifetime.Scoped).Select(s => s.ServiceType.ToString());
var transientGroup = _services.Where(s => s.Lifetime == ServiceLifetime.Transient).Select(s => s.ServiceType.ToString());
printGroup("scoped", "0", "blue", scopedGroup);
printGroup("transient", "1", "lightgrey", transientGroup);
// Make interfaces different
sb.AppendLine();
sb.AppendLine();
sb.AppendLine(" node [color=green]");
var interfacesGroup = _services.Where(s => s.ServiceType.IsInterface).Select(s => s.ServiceType.ToString());
foreach (var @interface in interfacesGroup)
{
sb.AppendLine($" \"{@interface}\"");
}
sb.AppendLine();
sb.AppendLine(" node [color=black]");
var noninterfacesGroup = _services.Where(s => !s.ServiceType.IsInterface).Select(s => s.ServiceType.ToString());
foreach (var nonInterface in noninterfacesGroup)
{
sb.AppendLine($" \"{nonInterface}\"");
}
//Now print dependencies
foreach (var d in dependencies)
{
sb.AppendLine($" \"{d.Key}\" -> \"{d.Value}\"");
}
sb.AppendLine("}");
sb.AppendLine("</pre>");
// Ok ready. Now return all the graph
await context.Response.WriteAsync(sb.ToString());
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.