Created
December 1, 2020 10:30
-
-
Save mikehadlow/b8641bddbe6028bd76c11cd6a24522a0 to your computer and use it in GitHub Desktop.
Microsoft.Extensions.DependencyInjection object graph writer.
This file contains 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
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"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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. HandlesIEnumerable<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
), yourIServiceCollection
, and either a writer function (such asSystem.Console.WriteLine
) or a file path.