Reference

AddTrax

Package Trax.EffectNamespaces Trax.Effect.Extensions, Trax.Effect.Configuration.TraxBuilderSource ServiceExtensions.cs, TraxBuilder.cs, TraxMarker.cs

Registers Trax on the service collection. The configure callback receives a TraxBuilder, on which the subsystems are added in a fixed order: AddEffects, then AddMediator, then AddScheduler. Every Trax host calls it once.

Signature

public static IServiceCollection AddTrax(
    this IServiceCollection services,
    Action<TraxBuilder> configure
)

Parameters

ParameterTypeDescription
servicesIServiceCollectionThe host's service collection
configureAction<TraxBuilder>Adds the subsystems and sets the host identity

Returns

IServiceCollection, the same collection, for chaining.

Behavior

configure runs first. Then AddTrax sets the process-wide host identity, TraxHostInfo.Current, from auto-detection and the host tracking methods called on the builder, and registers the services below. When configure does not call AddEffects, the effect configuration takes its defaults.

TraxBuilder

MemberDescription
AddEffects(...)The first step: configures effects and data providers. Returns TraxBuilderWithEffects. See AddEffects.
SetHostEnvironment, SetHostInstanceId, AddHostLabel, AddHostLabelsOverride or extend the host identity. See Host Tracking.
HasDataProviderTrue once UsePostgres, UseSqlite or UseInMemory ran. Set only by those methods.
HasDatabaseProviderTrue once UsePostgres or UseSqlite ran; false for UseInMemory. Set only by those methods.
ContributedMediatorAssembliesAssemblies a subsystem adds for AddMediator to scan, such as the state machine mutations' assembly AddStateMachines adds. AddMediator merges them into its scan.
MediatorConfiguredTrue once AddMediator has built the train registry.
ServiceCollectionThe service collection being configured.

Order of the steps

Each step returns a builder type that has only the next steps on it, so an out-of-order chain does not compile.

TypeReturned byNext step
TraxBuilderthe AddTrax callbackAddEffects()
TraxBuilderWithEffectsAddEffects()AddMediator()
TraxBuilderWithMediatorAddMediator(), AddScheduler()AddScheduler()

A call on the wrong step binds to an overload marked obsolete as an error, so the compiler reports the order (CS0619) instead of a missing method:

CallError
AddMediator before AddEffectsCall AddEffects(...) before AddMediator(...).
AddMediator a second timeAddMediator(...) is already called. Call it once and configure everything in that call.
AddScheduler before AddMediatorCall AddMediator(...) before AddScheduler(...).

Registered services

ServiceLifetimeRole
TraxMarkerSingletonRecords that AddTrax ran. AddTraxGraphQL() throws InvalidOperationException without it: AddTraxGraphQL() requires AddTrax() to be called first.
IEffectRegistrySingletonThe EffectRegistry every effect, junction effect and lifecycle hook is registered in, and toggled through
ITraxEffectConfigurationSingletonThe effect settings AddEffects built
IEffectRunner, IJunctionEffectRunner, ILifecycleHookRunnerTransientRun a train's effects, junction effects and lifecycle hooks
ILifecycleHookOutputPolicySingletonDefaultLifecycleHookOutputPolicy, unless SaveTrainParameters registered its own first
ITraxChangeSignal, TraxChangeSignalSingletonCollects the data-change signals behind onDataChanged. Its meter counts dropped signals as TraxChangeSignal.DroppedCounterName (trax.change_signal.dropped).
ChangeSignalCoalescerHosted serviceFlushes the collected signals to every registered IChangeSignalSink (FlushAsync), one per changed domain per window
ChangeSignalOptionsSingleton, if none is registeredCoalesceWindow (250 ms) and ChannelCapacity (1024). Register a configured instance before AddTrax to change them.
TimeProviderSingleton, if none is registeredTimeProvider.System
CheckpointOptions, IRunResumes, ICheckpointStoreSingleton, if none is registeredThe checkpoint store and resume path

Example

services.AddTrax(trax => trax
    .AddEffects(effects => effects
        .UsePostgres(connectionString)
        .AddJson()
        .SaveTrainParameters()
    )
    .AddMediator(typeof(IShipOrderTrain).Assembly)
    .AddScheduler()
);

See also