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.RegisterCommands(typeof(RunCommand).GetTypeInfo().Assembly);
    if (applicationAssembly != null)
    {
        factory.RegisterCommands(applicationAssembly);
    }

    // This method will direct the CommandFactory to go look for extension
    // assemblies with Oakton commands
    factory.RegisterCommandsFromExtensionAssemblies();

    factory.ConfigureRun = cmd =>
    {
        if (cmd.Input is IHostBuilderInput i)
        {
            factory.ApplyExtensions(source);
            i.HostBuilder = source;
        }
    };
});

snippet source | anchor