Table of Contents

Class RabbitMqMessage

Namespace
Savvyio.Extensions.RabbitMQ
Assembly
Savvyio.Extensions.RabbitMQ.dll

Provides a base class for RabbitMQ message operations, including connection and channel management, marshalling, and resource disposal. Ensures thread-safe initialization of RabbitMQ connectivity.

public abstract class RabbitMqMessage : AsyncDisposable, IDisposable, IAsyncDisposable, IAsyncHealthCheckProvider<IConnection>
Inheritance
RabbitMqMessage
Implements
Derived
Inherited Members

Constructors

RabbitMqMessage(IMarshaller, RabbitMqMessageOptions)

Initializes a new instance of the RabbitMqMessage class with the specified marshaller and options.

protected RabbitMqMessage(IMarshaller marshaller, RabbitMqMessageOptions options)

Parameters

marshaller IMarshaller

The marshaller used for serializing and deserializing messages.

options RabbitMqMessageOptions

The options used to configure the RabbitMQ connection.

Exceptions

ArgumentNullException

marshaller cannot be null -or- options cannot be null.

ArgumentException

options are not in a valid state.

Fields

MessageType

The header key used to indicate the message type in RabbitMQ properties.

protected const string MessageType = "type"

Field Value

string

Properties

Marshaller

Gets the by constructor provided serializer context.

protected IMarshaller Marshaller { get; }

Property Value

IMarshaller

The by constructor provided serializer context.

RabbitMqChannel

Gets the current RabbitMQ channel instance.

protected IChannel RabbitMqChannel { get; }

Property Value

IChannel

RabbitMqConnection

Gets the current RabbitMQ connection instance.

protected IConnection RabbitMqConnection { get; }

Property Value

IConnection

RabbitMqFactory

Gets the RabbitMQ connection factory used to create connections to the broker.

protected IConnectionFactory RabbitMqFactory { get; }

Property Value

IConnectionFactory

Methods

EnsureConnectivityAsync(CancellationToken)

Ensures that a connection and channel to the RabbitMQ broker are established and initialized. This method is thread-safe and will only initialize the connection and channel once.

protected Task EnsureConnectivityAsync(CancellationToken ct)

Parameters

ct CancellationToken

A CancellationToken that can be used to cancel the asynchronous operation.

Returns

Task

A Task representing the asynchronous operation.

Remarks

If the connection and channel are already initialized, this method returns immediately. Otherwise, it acquires an asynchronous lock to ensure only one initialization occurs, then creates the RabbitMQ connection and channel.

GetHealthCheckTargetAsync(CancellationToken)

Asynchronously returns an RabbitMQ.Client.IConnection instance used for probing the health status of the RabbitMQ broker.

public Task<IConnection> GetHealthCheckTargetAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A CancellationToken that can be used to cancel the asynchronous operation.

Returns

Task<IConnection>

A task that represents the asynchronous operation. The task result contains the RabbitMQ.Client.IConnection instance representing the active connection to the RabbitMQ broker, or a newly created connection if not already initialized.

OnDisposeManagedResourcesAsync()

Called when this object is being disposed by DisposeAsync(). Disposes the RabbitMQ channel and connection asynchronously if they have been initialized.

protected override ValueTask OnDisposeManagedResourcesAsync()

Returns

ValueTask

A ValueTask representing the asynchronous dispose operation.