Skip to content

JasperFx Options ​

JasperFxOptions is the central configuration class for JasperFx. It extends SystemPartBase and participates in the describe command output.

Registering Options ​

cs
public static void ConfigureOptions(IServiceCollection services)
{
    services.AddJasperFx(opts =>
    {
        // Register an environment check inline
        opts.RegisterEnvironmentCheck(
            "Database connectivity",
            async (sp, ct) =>
            {
                // Verify your database is accessible
                await Task.CompletedTask;
            });
    });
}

snippet source | anchor

Key Properties ​

PropertyTypeDefaultDescription
ActiveProfilestring"Development"The current application profile
RequiredFilesstring[][]Files that must exist at startup
RememberedApplicationAssemblyAssembly?nullOverride the detected application assembly
RememberApplicationAssemblybooltrueWhether this host records and reuses RememberedApplicationAssembly

Profiles ​

Settings that should differ between development and production live on a Profile. JasperFxOptions exposes a Development and a Production profile, and selects one as the ActiveProfile at startup based on the hosting environment (Development when IHostEnvironment.IsDevelopment(), otherwise Production).

csharp
services.AddJasperFx(opts =>
{
    // Fail fast locally so developers see migration problems immediately (this is the default)
    opts.Development.ResourceMigrationFailureMode = ResourceMigrationFailureMode.FailFast;

    // In production, don't let a transient startup migration failure crash the process
    opts.Production.ResourceMigrationFailureMode = ResourceMigrationFailureMode.ContinueOnFailures;
});

ResourceMigrationFailureMode ​

Controls what happens when a resource (database schema/migration, message broker objects, etc.) fails to set up or migrate during application startup through the resource setup hosted service (AddResourceSetupOnStartup, or ApplyAllDatabaseChangesOnStartup in the Critter Stack tools).

ValueBehavior
FailFast (default)A startup resource/migration failure throws and aborts application startup.
ContinueOnFailuresStartup resource/migration failures are logged but do not stop the application from starting.

ContinueOnFailures is intended for production multi-replica deployments. During a rolling deploy, several replicas can race for the migration advisory lock; a replica that loses the race would otherwise fail with "Unable to attain a global lock in time order to apply database changes" and crash-loop, even though the winning replica's committed migration has already made the schema current. With ContinueOnFailures that replica logs the failure and starts up against the now-current schema instead of crash-looping.

RequireFile ​

Tell JasperFx that a file path is required. This adds a file-exists check to environment tests:

csharp
opts.RequireFile("appsettings.json");

RegisterEnvironmentCheck ​

Register an inline environment check:

csharp
opts.RegisterEnvironmentCheck(
    "Redis is reachable",
    async (sp, ct) =>
    {
        // throw on failure
    });

Application Assembly Detection ​

JasperFx automatically detects the main application assembly for scanning. In test scenarios, you can override this:

csharp
JasperFxOptions.RememberedApplicationAssembly = typeof(MyApp).Assembly;

This is useful when test runners or IDEs change the entry assembly.

RememberedApplicationAssembly is a static, so it's shared by every host in the process. The first host to start without an explicit application assembly records its own there, and every later host that doesn't name one reuses it. In a test suite that starts several hosts, that makes type discovery depend on which test ran first. You have two ways out of that:

csharp
// This host neither records nor reuses the remembered assembly. Without an explicit
// ApplicationAssembly it uses the assembly it was registered from
services.AddJasperFx(opts => opts.RememberApplicationAssembly = false);

// Or clear the remembered assembly between hosts in a test fixture
JasperFxOptions.ResetRememberedApplicationAssembly();

Extensions that register JasperFx later ​

AddJasperFx() attributes the registration to its caller by walking the stack, and compares that assembly against the application assembly it adopts (ApplicationAssemblyReuseWarning). An extension whose entry point defers the call, for example into an IHostBuilder.ConfigureServices callback that runs inside Build(), should capture its caller up front and pass it in, because by the time the callback runs the stack belongs to whoever called Build():

csharp
public static IHostBuilder UseMyTool(this IHostBuilder builder)
{
    var registeredFrom = Assembly.GetCallingAssembly();
    return builder.ConfigureServices(services => services.AddJasperFx(configure: null, registeredFrom));
}

A null registeredFrom falls back to the stack walk.

Next Steps ​

Released under the MIT License.