Reference

AddServiceTrainBus

Package Trax.MediatorNamespace Trax.Mediator.ExtensionsSource ServiceExtensions.cs

The IServiceCollection registration that AddMediator performs: the train bus, the registry, discovery, execution, the concurrency limiter, the trusted scope, the startup checks, and every train found in the given assemblies. RegisterServiceTrains is the part of it that registers the trains alone.

Signatures

namespace Trax.Mediator.Extensions;
 
public static IServiceCollection AddServiceTrainBus(
    this IServiceCollection serviceCollection,
    ServiceLifetime serviceTrainLifetime = ServiceLifetime.Transient,
    params Assembly[] assemblies
)
 
public static IServiceCollection RegisterServiceTrains(
    this IServiceCollection services,
    ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
    params Assembly[] assemblies
)

Parameters

ParameterTypeDefaultDescription
serviceTrainLifetime / serviceLifetimeServiceLifetimeTransientThe lifetime each discovered train is registered with: Transient or Scoped
assembliesAssembly[]noneThe assemblies scanned for non-abstract classes implementing IServiceTrain<,>

Returns

The IServiceCollection, for chaining.

Behavior

AddMediator calls AddServiceTrainBus after registering its own MediatorConfiguration, and takes the mediator's settings (concurrency limits, AllowMissingAuthorizationService(), SkipChainVerification() and the rest), which AddServiceTrainBus has no parameter for. Called on its own, AddServiceTrainBus registers a MediatorConfiguration with the defaults (the given lifetime and assemblies, authorization required, chains verified, no concurrency limits) when none is registered yet. A MediatorConfiguration already registered is kept. Effects still come from AddTrax. Before Trax.Mediator 1.25.0 it registered no MediatorConfiguration, and a host built with it alone failed to resolve the services and startup checks that need one.

Both throw ArgumentException for ServiceLifetime.Singleton, because a train instance carries the state of the run in progress, and TrainException when a train implements two train interfaces, neither extending the other.

Registered services

ServiceLifetime
MediatorConfiguration, with the defaults, when none is registeredSingleton
ITrainRegistry (its InputTypeToTrain map is the first train scanned for each input type, which ITrainBus.RunAsync dispatches to), ITrainDiscoveryService, ITrainChainGraphs, IConcurrencyLimiter, ITrustedExecutionScope, ICurrentPrincipalProvider (returns null), IEnqueueContextAccessor, IWorkQueuePromotion, and the IServiceCollection itself, which discovery readsSingleton
ITrainBus, IRunExecutor (LocalRunExecutor), ITrainExecutionService, IInvokedTrainLauncher (queues the train a state machine's invoking state runs)Scoped
The startup chain check and the authorization registration checkHosted services, inserted ahead of every hosted service already registered
Each discovered train, under its own interface, through AddScopedTraxRoute or AddTransientTraxRouteThe lifetime given

RegisterServiceTrains registers only the last row. Each train is registered under its own interface (the one deriving from IServiceTrain<TIn, TOut>), or under the closed IServiceTrain<TIn, TOut> when it has none; see How Discovery Works.

Example

services.AddServiceTrainBus(ServiceLifetime.Scoped, typeof(OrderInput).Assembly);

See also