Class TransportConnectionBase
Abstract base class for transport connections providing TLS security verification.
public abstract class TransportConnectionBase : IAsyncDisposable
- Inheritance
-
TransportConnectionBase
- Implements
- Inherited Members
- Extension Methods
Remarks
This base class provides connection-time TLS validation to ensure transport security before any messages are sent.
Key security features:
- One-time TLS verification at connection establishment
- Cached verification result for zero per-message overhead
- Fail-fast behavior if TLS is not established
Implementations must override IsConnectionSecure() to verify the actual wire protocol security state for their specific transport.
Constructors
TransportConnectionBase(TransportSecurityOptions?)
Initializes a new instance of the TransportConnectionBase class.
protected TransportConnectionBase(TransportSecurityOptions? securityOptions = null)
Parameters
securityOptionsTransportSecurityOptionsThe security options for this transport connection.
Properties
SecurityOptions
Gets the transport options for this connection.
protected TransportSecurityOptions SecurityOptions { get; }
Property Value
TlsVerified
Gets a value indicating whether TLS verification has been performed and passed.
protected bool TlsVerified { get; }
Property Value
- bool
True if TLS has been verified; otherwise, false.
TransportLabel
When overridden in a derived class, gets the transport family this connection belongs to,
such as Kafka or RabbitMQ.
protected abstract string TransportLabel { get; }
Property Value
- string
The transport family name reported by TransportName.
Remarks
This is the family, not the implementing class and not the name a connection was registered under in dependency injection: a consumer branching on TransportName must see the same value whichever code path refused the connection. It is abstract rather than defaulted so a derived transport cannot report its class name by omission.
Methods
ConnectAsync(CancellationToken)
Establishes a connection to the transport and verifies TLS security.
public Task ConnectAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task
A task representing the asynchronous operation.
Remarks
This method performs TLS verification ONCE at connection establishment. The result is cached to ensure zero per-message overhead.
TLS validation happens at connection time because:
- Cloud transports (SQS, Service Bus, Pub/Sub) enforce TLS - nothing to validate
- RabbitMQ/Kafka: config might say TLS but connection could fail/downgrade
- Verifies actual wire protocol, not just settings
- One-time cost during connection setup (no per-message overhead)
Exceptions
- TransportSecurityException
Thrown when RequireTls is true and IsConnectionSecure() returns false.
DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
public ValueTask DisposeAsync()
Returns
- ValueTask
A task that represents the asynchronous dispose operation.
DisposeCoreAsync()
When overridden in a derived class, releases transport-specific resources.
protected virtual ValueTask DisposeCoreAsync()
Returns
- ValueTask
A task representing the asynchronous operation.
EstablishConnectionAsync(CancellationToken)
When overridden in a derived class, establishes the underlying transport connection.
protected abstract Task EstablishConnectionAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task
A task representing the asynchronous operation.
Remarks
Implementations should establish the actual connection to the message broker (e.g., RabbitMQ connection, Kafka producer, cloud service client). TLS verification happens automatically after this method completes.
IsConnectionSecure()
When overridden in a derived class, verifies that the connection is secured with TLS.
protected abstract bool IsConnectionSecure()
Returns
- bool
True if the connection is secured with TLS; otherwise, false.
Remarks
This method is called once at connection establishment to verify the actual wire protocol security state.
Implementation examples:
RabbitMQ:
protected override bool IsConnectionSecure()
=> _connection.Endpoint.Ssl.Enabled && _connection.IsOpen;
Kafka:
protected override bool IsConnectionSecure()
=> _producerConfig.SecurityProtocol == SecurityProtocol.Ssl
|| _producerConfig.SecurityProtocol == SecurityProtocol.SaslSsl;
Cloud Transports (SQS, Service Bus, Pub/Sub):
protected override bool IsConnectionSecure() => true; // Always TLS