Skip to content

Instantly share code, notes, and snippets.

@carmeleve
Created August 3, 2020 08:45
Embed
What would you like to do?
public static class CliCommandCollectionExtensions
{
public static IServiceCollection AddCliCommands(this IServiceCollection services)
{
Type greetCommandType = typeof(DeployCommand);
Type commandType = typeof(Command);
IEnumerable<Type> commands = greetCommandType
.Assembly
.GetExportedTypes()
.Where(x => x.Namespace == greetCommandType.Namespace && commandType.IsAssignableFrom(x));
foreach (Type command in commands)
{
services.AddSingleton(commandType, command);
}
services.AddSingleton(sp =>
{
return
sp.GetRequiredService<IConfiguration>().GetSection("Deployment").Get<DeploymentOptions>()
?? throw new ArgumentException("Deployment configuration cannot be missing.");
});
return services;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment