Bootstrapping & Configuration Edit on GitHub


Note! Jasper uses the ASP.Net Core IWebHostBuilder infrastructure internally for bootstrapping now, even for idiomatic Jasper bootstrapping.

All the examples in this page are using the default, "in the box" options for Jasper. To see what else can be configured or added to a Jasper application, see the folling topics:

Even if running "headless" (i.e., without Kestrel), Jasper applications are effectively ASP.Net Core applications and use the ASP.Net Core IWebHostBuilder for all bootstrapping and application lifecycle events.

In its simplest possible setup, you can fire up a Jasper application in memory like so:


var host = WebHost.CreateDefaultBuilder()
    .UseStartup<Startup>()
    .UseJasper() // Use Jasper with all its
                 // defaults
    .Start();

More likely though, you'll want to run a Jasper-ized ASP.net Core application from a command line application. Jasper goes all in on command line tooling with quite a bit of its own diagnostics, so naturally it comes with a first class citizen for bootstrapping and executing from the command line with JasperHost like so:


public static int Main(params string[] args)
{
    var builder = WebHost.CreateDefaultBuilder()
        .UseStartup<Startup>()
        .UseJasper();

    return JasperAgent.Run(builder, args);
}

Or with the usage of an extension method in Jasper, this is an exact equivalent:


public static int Main(params string[] args)
{
    return WebHost.CreateDefaultBuilder()
        .UseStartup<Startup>()
        .UseJasper()
        .RunJasper(args);
}

Headless Applications

If you are building a Jasper application that does not expose any HTTP endpoints or needs to customize the underlying IWebHostBuilder, you can use JasperHost.CreateDefaultBuilder() as shown below to create a pre-configured IWebHostBuilder that is lighter than WebHost.CreateDefaultBuilder() that you would use for HTTP projects:


using (var host = JasperHost.CreateDefaultBuilder()
    .UseJasper()
    .StartJasper())
{
    // do stuff
}

This default IWebHostBuilder behind the scenes is this:


return new WebHostBuilder()
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
        var env = hostingContext.HostingEnvironment;

        config
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

        config.AddEnvironmentVariables();

    })
    .ConfigureLogging((hostingContext, logging) =>
    {
        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
        logging.AddConsole();
        logging.AddDebug();

    })
    .ConfigureServices(s =>
    {
        // Registers an empty startup if there is none in the application
        if (s.All(x => x.ServiceType != typeof(IStartup)))
        {
            s.AddSingleton<IStartup>(new NulloStartup());
        }

        // Registers a "nullo" server if there is none in the application
        // i.e., Kestrel isn't applied
        if (s.All(x => x.ServiceType != typeof(IServer)))
        {
            s.AddSingleton<IServer>(new NulloServer());
        }

        // This guarantees that the Jasper middleware is part of the RequestDelegate
        // at the end if it has not been explicitly added
        s.AddSingleton<IStartupFilter>(new RegisterJasperStartupFilter());
    });

There is also a shortcut for bootstrapping directly with JasperHost like this:


using (var host = JasperHost.Basic())
{
    // do stuff
}

Which is just syntactical sugar for:


using (var host = JasperHost.CreateDefaultBuilder()
    .UseJasper()
    .StartJasper())
{
    // do stuff
}

And likewise, to run a Jasper application from a command line application, you can again use JasperRuntime like so:


public static int Main(params string[] args)
{
    return JasperAgent.RunBasic(args);
}

This option might be enough to do some useful things with Jasper as a command executor at the least, but more likely you'll want to add other elements to your system like additional services to the underlying IoC container, the messaging configuration, or ASP.Net Core middleware.

All configuration and set up of Jasper starts with the JasperRegistry class. Typically you would subclass JasperRegistry, but if you have only minimal configuration needs, you might bootstrap like this:


using (var runtime = JasperHost.For(_ =>
{
    _.Transports.ListenForMessagesFrom("tcp://localhost:2001");
}))
{
    // do stuff
}

More likely though is that you will opt to define your application with a custom JasperRegistry:


public class MyJasperApp : JasperRegistry
{
    public MyJasperApp()
    {
        ServiceName = "My Jasper App";

        Transports.ListenForMessagesFrom("durable://localhost:2111/incoming");
    }
}

And then use that to bootstrap your application:


using (var runtime = JasperHost.For<MyJasperApp>())
{
    // do stuff
}

// or

using (var runtime = JasperHost.For(new MyJasperApp()))
{
    // do stuff
}