Class TransportAdapterHostedService
Hosted service that manages the lifecycle of transport adapters.
public sealed class TransportAdapterHostedService : ITransportLifecycleManager, IHostedService
- Inheritance
-
TransportAdapterHostedService
- Implements
- Inherited Members
- Extension Methods
Remarks
This service ensures transport adapters are properly started when the application starts and gracefully stopped when the application shuts down. Adapters are started in registration order and stopped in reverse order to ensure proper dependency handling.
During shutdown, the service allows a configurable drain period for pending messages before forcefully stopping adapters.
Factory-registered transports are initialized using the service provider during startup, before starting any adapters.
In addition to automatic lifecycle management via IHostedService, this class implements ITransportLifecycleManager to provide runtime control over individual transports for scenarios like graceful degradation, dynamic scaling, and testing.
Constructors
TransportAdapterHostedService(ITransportRegistry, IOptions<TransportAdapterHostedServiceOptions>, IServiceProvider, ILogger<TransportAdapterHostedService>)
Initializes a new instance of the TransportAdapterHostedService class.
public TransportAdapterHostedService(ITransportRegistry transportRegistry, IOptions<TransportAdapterHostedServiceOptions> options, IServiceProvider serviceProvider, ILogger<TransportAdapterHostedService> logger)
Parameters
transportRegistryITransportRegistryThe transport registry containing registered adapters.
optionsIOptions<TransportAdapterHostedServiceOptions>The hosted service options.
serviceProviderIServiceProviderThe service provider for factory-based transport creation.
loggerILogger<TransportAdapterHostedService>The logger.
Properties
RegisteredTransports
Gets all registered transport adapters.
public IReadOnlyCollection<ITransportAdapter> RegisteredTransports { get; }
Property Value
- IReadOnlyCollection<ITransportAdapter>
A read-only collection of transport adapters that have been registered with the lifecycle manager.
Remarks
This collection includes both running and stopped transports. Use IsRunning to check individual transport status.
TransportNames
Gets all transport names that are currently registered.
public IReadOnlyCollection<string> TransportNames { get; }
Property Value
- IReadOnlyCollection<string>
A read-only collection of transport names.
Methods
GetTransport(string)
public ITransportAdapter? GetTransport(string name)
Parameters
namestring
Returns
IsRegistered(string)
Checks if a transport with the specified name is registered.
public bool IsRegistered(string name)
Parameters
namestringThe name of the transport to check.
Returns
Exceptions
- ArgumentException
Thrown when
nameis null or empty.
IsRunning(string)
public bool IsRunning(string name)
Parameters
namestring
Returns
StartAsync(CancellationToken)
Triggered when the application host is ready to start the service.
public Task StartAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenIndicates that the start process has been aborted.
Returns
StartTransportAsync(string, CancellationToken)
Starts a specific transport adapter by name.
public Task StartTransportAsync(string name, CancellationToken cancellationToken)
Parameters
namestringThe name of the transport to start.
cancellationTokenCancellationTokenCancellation token for the operation.
Returns
- Task
A task representing the asynchronous start operation.
Remarks
If the transport is already running, this method returns immediately without error.
Exceptions
- ArgumentException
Thrown when
nameis null or empty.- InvalidOperationException
Thrown when no transport with the specified name is registered.
StopAsync(CancellationToken)
Triggered when the application host is performing a graceful shutdown.
public Task StopAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenIndicates that the shutdown process should no longer be graceful.
Returns
StopTransportAsync(string, CancellationToken)
Stops a specific transport adapter by name.
public Task StopTransportAsync(string name, CancellationToken cancellationToken)
Parameters
namestringThe name of the transport to stop.
cancellationTokenCancellationTokenCancellation token for the operation.
Returns
- Task
A task representing the asynchronous stop operation.
Remarks
If the transport is already stopped, this method returns immediately without error.
The stop operation includes a drain period to allow in-flight messages to complete processing before forcing shutdown.
Exceptions
- ArgumentException
Thrown when
nameis null or empty.- InvalidOperationException
Thrown when no transport with the specified name is registered.