Oakton 3.0.0


Next

Improved "Run" Command

Previous

Commands

Integration with HostBuilder Edit on GitHub


Note! The functionality to integrate Oakton into .Net Core projects in Oakton.AspNetCore was combined into the main Oakton library for V3.0.

Oakton works well with the generic HostBuilder in .Net Core and .Net 5 to extend the command line support of your applications.

To enable the extended command line options in your .Net application bootstrapped by IHostBuilder, first install the Oakton nuget to your project. Then modify the Program.Main() method generated by the typical .Net project templates:


public class Program
{
    public static Task<int> Main(string[] args)
    {
        return CreateHostBuilder(args)
            
            // This extension method replaces the calls to
            // IWebHost.Build() and Start()
            .RunOaktonCommands(args);
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(x => x.UseStartup<Startup>());
    
}

or with IWebHostBuilder:


public class Program
{
    public static Task<int> Main(string[] args)
    {
        return CreateWebHostBuilder(args)
            
            // This extension method replaces the calls to
            // IWebHost.Build() and Start()
            .RunOaktonCommands(args);
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
    
}

There are just a couple things to note:

  1. The return value of the Program.Main() method now needs to be Task<int> rather than void. This is done so that Oakton can return either successful or failure exit codes for usage in diagnostic commands you may want to stop automated builds upon failures.
  2. You will use the RunOaktonCommands() method to accept the command line arguments and invoke your system rather than manually building and/or starting the IWebHost yourself