Train discovery
Source TrainRegistry/, TrainBus/, TrainDiscoveryService.cs, TrainExecutionService.cs
How AddMediator finds trains and how the train bus picks one. Why the bus routes by input type
is in Mediator.
The registry scan
The train registry scans each assembly passed to AddMediator, in the order given (an assembly
passed twice is scanned once), for non-abstract classes that implement IServiceTrain<TIn, TOut>.
For each class it selects a service type:
| The class implements | Service type |
|---|---|
One non-generic interface deriving from IServiceTrain<TIn, TOut> (IMyTrain) | That interface |
| Several such interfaces, one extending the others | The most derived one |
| Several such interfaces, none extending the others | None: TrainException, "implements more than one train interface" |
| No such interface | The closed IServiceTrain<TIn, TOut> |
The service type is the train's registration and its canonical name (its FullName). The
registry then maps each service type's TIn to the service type. When two trains take the same
input type, the first one found keeps the mapping: assemblies in the order given, then each
assembly's own type order. Both trains are still registered and discovered.
Running by input type
ITrainBus.RunAsync<TOut>(input, ...):
- Looks up
input.GetType()in the registry. The lookup uses the exact runtime type, so an input of a derived type finds only a train registered for that derived type. No entry throwsNoTrainForInputException, whose message names the scanned assemblies (see Troubleshooting). - Creates a DI scope and resolves the service type in it.
- Fills the train's
[Inject]properties. - Calls the train's
Run. With nometadatathe train creates its own record. With one, which must bePending(any other state throwsTrainException), it runs as that pre-created record, the way the scheduler runs a dispatched job. The metadata is not a parent link, and nothing sets the run'sParentId. - Disposes the scope when the run returns.
Running by name
ITrainBus.RunByNameAsync(trainName, input, ...) takes the canonical name only. A name no train
is registered under throws TrainException "No train is registered under the name (X)", and an
input that is not an instance of the train's input type throws TrainException. It then resolves
and runs the train in a scope of its own, as RunAsync does.
ITrainExecutionService finds a train by name in this order:
- Canonical name: the service type's
FullName, such asMyApp.Trains.IProcessOrderTrain. - Friendly name: the registration's
ServiceTypeName. A name that matches more than one train's friendly name throwsAmbiguousTrainNameException.
There is no short-name fallback. No match throws TrainNotFoundException, whose message is always
"The requested train was not found.". The canonical name is what metadata and work queue entries
store, and it does not change when the implementing class is renamed.
Discovery rules
| Rule | When broken |
|---|---|
| The class is concrete | An abstract class is not scanned |
The class implements IServiceTrain<,> | The class is not scanned |
| The class has a public constructor the container can satisfy | The startup chain check refuses the host, naming the missing type |
| One class per class service type | Two classes registered under one class service type (AddTransient<BaseTrain, A>() and AddTransient<BaseTrain, B>()) make DiscoverTrains throw TrainException, so the host fails at startup |
SDK Reference
AddMediator | ITrainBus | ITrainDiscoveryService | ITrainExecutionService