Table of Contents

Class TransportAdapterHostedService

Namespace
Excalibur.Dispatch.Transport
Assembly
Excalibur.Dispatch.dll

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

transportRegistry ITransportRegistry

The transport registry containing registered adapters.

options IOptions<TransportAdapterHostedServiceOptions>

The hosted service options.

serviceProvider IServiceProvider

The service provider for factory-based transport creation.

logger ILogger<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

name string

Returns

ITransportAdapter

IsRegistered(string)

Checks if a transport with the specified name is registered.

public bool IsRegistered(string name)

Parameters

name string

The name of the transport to check.

Returns

bool

true if a transport with the specified name is registered; otherwise, false.

Exceptions

ArgumentException

Thrown when name is null or empty.

IsRunning(string)

public bool IsRunning(string name)

Parameters

name string

Returns

bool

StartAsync(CancellationToken)

Triggered when the application host is ready to start the service.

public Task StartAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Indicates that the start process has been aborted.

Returns

Task

A Task that represents the asynchronous Start operation.

StartTransportAsync(string, CancellationToken)

Starts a specific transport adapter by name.

public Task StartTransportAsync(string name, CancellationToken cancellationToken)

Parameters

name string

The name of the transport to start.

cancellationToken CancellationToken

Cancellation 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 name is 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

cancellationToken CancellationToken

Indicates that the shutdown process should no longer be graceful.

Returns

Task

A Task that represents the asynchronous Stop operation.

StopTransportAsync(string, CancellationToken)

Stops a specific transport adapter by name.

public Task StopTransportAsync(string name, CancellationToken cancellationToken)

Parameters

name string

The name of the transport to stop.

cancellationToken CancellationToken

Cancellation 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 name is null or empty.

InvalidOperationException

Thrown when no transport with the specified name is registered.