Skip to content

Instantly share code, notes, and snippets.

@mikehadlow
Created December 1, 2020 10:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikehadlow/b8641bddbe6028bd76c11cd6a24522a0 to your computer and use it in GitHub Desktop.
Save mikehadlow/b8641bddbe6028bd76c11cd6a24522a0 to your computer and use it in GitHub Desktop.
Microsoft.Extensions.DependencyInjection object graph writer.
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MyNamespace
{
public class ServiceCollectionWriter
{
public static void WriteObjectGraph<TRoot>(IServiceCollection services, string path)
{
using var writer = new StreamWriter(File.OpenWrite(path));
WriteObjectGraph<TRoot>(services, writer.WriteLine);
}
public static void WriteObjectGraph<TRoot>(IServiceCollection services, Action<string> writeLine)
{
var serviceLookup = new Dictionary<Type, List<ServiceDescriptor>>();
foreach(var descriptor in services)
{
if(serviceLookup.ContainsKey(descriptor.ServiceType))
{
serviceLookup[descriptor.ServiceType].Add(descriptor);
}
else
{
serviceLookup.Add(descriptor.ServiceType, new List<ServiceDescriptor> { descriptor });
}
}
WriteNode(typeof(TRoot), 0);
void WriteNode(Type serviceType, int indent)
{
var tab = new string(' ', indent * 2);
if(serviceLookup.ContainsKey(serviceType))
{
foreach (var descriptor in serviceLookup[serviceType])
{
if (descriptor.ImplementationType != null)
{
writeLine($"{tab}{descriptor.ServiceType.Name} -> {descriptor.ImplementationType.Name}");
writeLine($"{tab}{{");
var parameters = descriptor.ImplementationType.GetConstructors().First().GetParameters();
foreach (var parameter in parameters)
{
if(parameter.ParameterType.IsGenericType
&& parameter.ParameterType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
WriteNode( parameter.ParameterType.GetGenericArguments().First(), indent + 1);
}
else
{
WriteNode(parameter.ParameterType, indent + 1);
}
}
writeLine($"{tab}}}");
}
else if (descriptor.ImplementationFactory != null)
{
writeLine($"{tab}{serviceType.Name} -> FACTORY");
}
else if (descriptor.ImplementationInstance != null)
{
writeLine($"{tab}{serviceType.Name} -> INSTANCE");
}
else
{
writeLine($"{tab}{serviceType.Name} -> UNKNOWN");
}
}
}
else
{
writeLine($"{tab}{serviceType.Name} -> NOT RESOLVED");
}
}
}
}
}
@mikehadlow
Copy link
Author

mikehadlow commented Dec 1, 2020

This writes out a textural representation of an object graph based on the services registered to your IServiceCollection. It's very very basic, and works simply by picking the first constructor of each registered implementation and reflecting over its arguments. Handles IEnumerable<T> arguments as well. It should be used for information only. There are no guarantees that it will show your actual application's object graph correctly.

It outputs a custom format, but one which displays nicely when opened in VS Code, and allows nodes to be opened and closed, which is great for large graphs.

Copy this file into your application and call it after registering your services, specifying the root of your graph (here IHostedService), your IServiceCollection, and either a writer function (such as System.Console.WriteLine) or a file path.

ServiceCollectionWriter.WriteObjectGraph<IHostedService>(services, "C:/Temp/my-object-graph.txt");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment