Skip to content
On this page

Command Assembly Discovery

This feature probably won't be commonly used, but there is a mechanism to automatically find and load Oakton commands from other assemblies through file scanning.

The first step is to mark any assembly containing Oakton commands you want discovered and loaded through this mechanism with this attribute:

cs
[assembly:Oakton.OaktonCommandAssembly]

snippet source | anchor

Next, when you build a CommandFactory, you need to explicitly opt into the auto-discovery of commands by using the RegisterCommandsFromExtensionAssemblies() option as shown below in the Oakton.AspNetCore code:

cs
return CommandExecutor.For(factory =>
{
    factory.ApplyFactoryDefaults(applicationAssembly);

    factory.ConfigureRun = commandRun =>
    {
        if (commandRun.Input is IHostBuilderInput i)
        {
            factory.ApplyExtensions(source);
            i.HostBuilder = source;
        }
        else
        {
            var props = commandRun.Command.GetType().GetProperties().Where(x => x.HasAttribute<InjectServiceAttribute>())
                .ToArray();

            if (props.Any())
            {
                commandRun.Command = new HostWrapperCommand(commandRun.Command, source.Build, props);
            }
        }
        
        
    };
});

snippet source | anchor