StellaOps.Router.Transport.RabbitMq

Router RabbitMQ transport, client side: connects a microservice to a gateway through RabbitMQ queues.

library
Component
Router
Licence
Apache-2.0
Target framework
net10.0
Latest release
1.0.1-20261117T070237Z

Install

dotnet add package StellaOps.Router.Transport.RabbitMq --version 1.0.1-20261117T070237Z

Namespaces

NamespaceTypes
StellaOps.Router.Transport.RabbitMq5

namespace StellaOps.Router.Transport.RabbitMq

TypeKindDescription
RabbitMqFrameProtocolclassHandles serialization and deserialization of frames for RabbitMQ transport.
RabbitMqTransportClientclassRabbitMQ transport client implementation for microservices.
RabbitMqTransportClientServiceCollectionExtensionsclassExtension methods for registering RabbitMQ transport services.
RabbitMqTransportOptionsclassOptions for RabbitMQ transport configuration.
RabbitMqTransportPluginclassPlugin for the client side of the RabbitMQ transport, which a microservice connects with.

class RabbitMqFrameProtocol

public static class RabbitMqFrameProtocol

Handles serialization and deserialization of frames for RabbitMQ transport.

Members

MemberKindSummary
CreateProperties(Frame, string, TimeSpan?)methodCreates BasicProperties for a frame.
ExtractConnectionId(IReadOnlyBasicProperties)methodExtracts the connection ID from message properties.
ParseFrame(ReadOnlyMemory<byte>, IReadOnlyBasicProperties)methodParses a frame from a RabbitMQ message.

method CreateProperties(Frame, string, TimeSpan?) #

public static BasicProperties CreateProperties(Frame frame, string? replyTo, TimeSpan? timeout = null)

Creates BasicProperties for a frame.

Parameters

  • frame: The frame to serialize.
  • replyTo: The reply queue name.
  • timeout: Optional timeout for the message.

Returns

The basic properties.

method ExtractConnectionId(IReadOnlyBasicProperties) #

public static string ExtractConnectionId(IReadOnlyBasicProperties properties)

Extracts the connection ID from message properties.

Parameters

  • properties: The message properties.

Returns

The connection ID.

method ParseFrame(ReadOnlyMemory<byte>, IReadOnlyBasicProperties) #

public static Frame ParseFrame(ReadOnlyMemory<byte> body, IReadOnlyBasicProperties properties)

Parses a frame from a RabbitMQ message.

Parameters

  • body: The message body. The frame keeps a copy: RabbitMQ.Client releases a delivery body when the ReceivedAsync handler returns, and frames are read later.
  • properties: The message properties.

Returns

The parsed frame.

class RabbitMqTransportClient

public sealed class RabbitMqTransportClient : ITransportClient, IMicroserviceTransport, IAsyncDisposable

RabbitMQ transport client implementation for microservices.

Members

MemberKindSummary
RabbitMqTransportClient(IOptions<RabbitMqTransportOptions>, ILogger<RabbitMqTransportClient>, IFrameEnvelopeSigner, IFrameEnvelopeVerifier)constructorInitializes a new instance of the RabbitMqTransportClientclass.
OnCancelReceivedeventEvent raised when a CANCEL frame is received.
OnRequestReceivedeventEvent raised when a REQUEST frame is received.
CancelAllInflight(string)methodCancels all in-flight handlers.
ConnectAsync(InstanceDescriptor, IReadOnlyList<EndpointDescriptor>, CancellationToken)methodConnects to the gateway via RabbitMQ.
ConnectAsync(InstanceDescriptor, IReadOnlyList<EndpointDescriptor>, IReadOnlyDictionary<string, SchemaDefinition>, ServiceOpenApiInfo, CancellationToken)methodConnects to the gateway via RabbitMQ and registers the instance, its endpoints, schemas and OpenAPI metadata in the HELLO message.
DisconnectAsync()methodDisconnects from the gateway.
DisposeAsync()methodSee IAsyncDisposable.DisposeAsync().
SendCancelAsync(ConnectionState, Guid, string)methodSends a cancellation request.
SendHeartbeatAsync(HeartbeatPayload, CancellationToken)methodSends a heartbeat.
SendRequestAsync(ConnectionState, Frame, TimeSpan, CancellationToken)methodSends a request and waits for a response.
SendStreamingAsync(ConnectionState, Frame, Stream, Func<ResponseFrame, Stream, Task>, PayloadLimits, CancellationToken)methodSends a request whose body is read from a stream and hands the microservice’s response to readResponse.

constructor RabbitMqTransportClient(IOptions<RabbitMqTransportOptions>, ILogger<RabbitMqTransportClient>, IFrameEnvelopeSigner, IFrameEnvelopeVerifier) #

public RabbitMqTransportClient(IOptions<RabbitMqTransportOptions> options, ILogger<RabbitMqTransportClient> logger, IFrameEnvelopeSigner? envelopeSigner = null, IFrameEnvelopeVerifier? envelopeVerifier = null)

Initializes a new instance of the RabbitMqTransportClientclass.

event OnCancelReceived #

public event Func<Guid, string?, Task>? OnCancelReceived

Event raised when a CANCEL frame is received.

event OnRequestReceived #

public event Func<Frame, CancellationToken, Task<Frame>>? OnRequestReceived

Event raised when a REQUEST frame is received.

method CancelAllInflight(string) #

public void CancelAllInflight(string reason)

Cancels all in-flight handlers.

method ConnectAsync(InstanceDescriptor, IReadOnlyList<EndpointDescriptor>, CancellationToken) #

public Task ConnectAsync(InstanceDescriptor instance, IReadOnlyList<EndpointDescriptor> endpoints, CancellationToken cancellationToken)

Connects to the gateway via RabbitMQ.

Parameters

  • instance: The instance descriptor.
  • endpoints: The endpoints to register.
  • cancellationToken: Cancellation token.

method ConnectAsync(InstanceDescriptor, IReadOnlyList<EndpointDescriptor>, IReadOnlyDictionary<string, SchemaDefinition>, ServiceOpenApiInfo, CancellationToken) #

public Task ConnectAsync(InstanceDescriptor instance, IReadOnlyList<EndpointDescriptor> endpoints, IReadOnlyDictionary<string, SchemaDefinition>? schemas, ServiceOpenApiInfo? openApiInfo, CancellationToken cancellationToken)

Connects to the gateway via RabbitMQ and registers the instance, its endpoints, schemas and OpenAPI metadata in the HELLO message.

Parameters

  • instance: The instance descriptor.
  • endpoints: The endpoints to register.
  • schemas: Schema definitions referenced by the endpoints.
  • openApiInfo: OpenAPI metadata for the service.
  • cancellationToken: Cancellation token.

method DisconnectAsync() #

public Task DisconnectAsync()

Disconnects from the gateway.

method DisposeAsync() #

public ValueTask DisposeAsync()

See IAsyncDisposable.DisposeAsync().

method SendCancelAsync(ConnectionState, Guid, string) #

public Task SendCancelAsync(ConnectionState connection, Guid correlationId, string? reason = null)

Sends a cancellation request.

Parameters

  • connection: The connection to use.
  • correlationId: The correlation ID of the request to cancel.
  • reason: Optional reason for cancellation.

method SendHeartbeatAsync(HeartbeatPayload, CancellationToken) #

public Task SendHeartbeatAsync(HeartbeatPayload heartbeat, CancellationToken cancellationToken)

Sends a heartbeat.

method SendRequestAsync(ConnectionState, Frame, TimeSpan, CancellationToken) #

public Task<Frame> SendRequestAsync(ConnectionState connection, Frame requestFrame, TimeSpan timeout, CancellationToken cancellationToken)

Sends a request and waits for a response.

Parameters

  • connection: The connection to use.
  • requestFrame: The request frame.
  • timeout: The timeout for the request.
  • cancellationToken: Cancellation token.

Returns

The response frame.

method SendStreamingAsync(ConnectionState, Frame, Stream, Func<ResponseFrame, Stream, Task>, PayloadLimits, CancellationToken) #

public Task SendStreamingAsync(ConnectionState connection, Frame requestHeader, Stream requestBody, Func<ResponseFrame, Stream, Task> readResponse, PayloadLimits limits, CancellationToken cancellationToken)

Sends a request whose body is read from a stream and hands the microservice’s response to readResponse. TransportStreamingDispatch.SendBufferedAsync(ITransportClient, ConnectionState, Frame, Stream, Func<ResponseFrame, Stream, Task>, PayloadLimits, CancellationToken)implements it for transports whose microservice side answers a request with one response frame.

Parameters

  • connection: The connection to use.
  • requestHeader: The request header frame.
  • requestBody: The request body stream.
  • readResponse: Callback that receives the response head (the status code and headers of the microservice’s response; its ResponseFrame.Payloadis empty) and the response body stream.
  • limits: Payload limits to enforce.
  • cancellationToken: Cancellation token.

class RabbitMqTransportClientServiceCollectionExtensions

public static class RabbitMqTransportClientServiceCollectionExtensions

Extension methods for registering RabbitMQ transport services.

Members

MemberKindSummary
AddRabbitMqTransportClient(IServiceCollection, Action<RabbitMqTransportOptions>)extension methodAdds RabbitMQ transport client services to the service collection.

extension method AddRabbitMqTransportClient(IServiceCollection, Action<RabbitMqTransportOptions>) #

public static IServiceCollection AddRabbitMqTransportClient(this IServiceCollection services, Action<RabbitMqTransportOptions>? configure = null)

Adds RabbitMQ transport client services to the service collection.

Parameters

  • services: The service collection.
  • configure: Optional configuration action.

Returns

The service collection.

class RabbitMqTransportOptions

public sealed class RabbitMqTransportOptions

Options for RabbitMQ transport configuration.

Members

MemberKindSummary
RabbitMqTransportOptions()constructorCreates an instance of RabbitMqTransportOptions.
AutoDeleteQueuespropertyGets or sets whether queues should auto-delete on disconnect.
AutomaticRecoveryEnabledpropertyGets or sets whether to use automatic recovery.
DefaultTimeoutpropertyGets or sets the default request timeout.
DurableQueuespropertyGets or sets whether queues should be durable.
ExchangePrefixpropertyGets or sets the exchange prefix.
HostNamepropertyGets or sets the RabbitMQ host name.
InstanceIdpropertyGets or sets the instance ID for this microservice instance.
MaxMessageSizepropertyGets or sets the largest message, in bytes, this side publishes; a larger frame is refused before it is published (PayloadTooLargeException): the gateway answers 413 for a request and the service 502 for a response, instead of a broker refusal that closes the channel.
NetworkRecoveryIntervalpropertyGets or sets the network recovery interval.
NodeIdpropertyGets or sets the node ID for this gateway instance.
PasswordpropertyGets or sets the RabbitMQ password.
PortpropertyGets or sets the RabbitMQ port.
PrefetchCountpropertyGets or sets the prefetch count (concurrent messages).
QueuePrefixpropertyGets or sets the queue prefix.
RequestExchangepropertyGets or sets the request exchange name.
ResponseExchangepropertyGets or sets the response exchange name.
SslCertPathpropertyGets or sets the SSL certificate path.
UseSslpropertyGets or sets whether to use SSL/TLS.
UserNamepropertyGets or sets the RabbitMQ username.
VirtualHostpropertyGets or sets the RabbitMQ virtual host.

constructor RabbitMqTransportOptions() #

public RabbitMqTransportOptions()

Creates an instance of RabbitMqTransportOptions.

property AutoDeleteQueues #

public bool AutoDeleteQueues { get; set; }

Gets or sets whether queues should auto-delete on disconnect.

property AutomaticRecoveryEnabled #

public bool AutomaticRecoveryEnabled { get; set; }

Gets or sets whether to use automatic recovery.

property DefaultTimeout #

public TimeSpan DefaultTimeout { get; set; }

Gets or sets the default request timeout.

property DurableQueues #

public bool DurableQueues { get; set; }

Gets or sets whether queues should be durable.

property ExchangePrefix #

public string ExchangePrefix { get; set; }

Gets or sets the exchange prefix.

property HostName #

public string HostName { get; set; }

Gets or sets the RabbitMQ host name.

property InstanceId #

public string? InstanceId { get; set; }

Gets or sets the instance ID for this microservice instance.

property MaxMessageSize #

public int MaxMessageSize { get; set; }

Gets or sets the largest message, in bytes, this side publishes; a larger frame is refused before it is published (PayloadTooLargeException): the gateway answers 413 for a request and the service 502 for a response, instead of a broker refusal that closes the channel. Keep it at or below the broker’s max_message_size. Default: 16 MiB (16777216), the broker default of RabbitMQ 4.

property NetworkRecoveryInterval #

public TimeSpan NetworkRecoveryInterval { get; set; }

Gets or sets the network recovery interval.

property NodeId #

public string? NodeId { get; set; }

Gets or sets the node ID for this gateway instance.

property Password #

public string Password { get; set; }

Gets or sets the RabbitMQ password.

property Port #

public int Port { get; set; }

Gets or sets the RabbitMQ port.

property PrefetchCount #

public ushort PrefetchCount { get; set; }

Gets or sets the prefetch count (concurrent messages).

property QueuePrefix #

public string QueuePrefix { get; set; }

Gets or sets the queue prefix.

property RequestExchange #

public string RequestExchange { get; }

Gets or sets the request exchange name.

property ResponseExchange #

public string ResponseExchange { get; }

Gets or sets the response exchange name.

property SslCertPath #

public string? SslCertPath { get; set; }

Gets or sets the SSL certificate path.

property UseSsl #

public bool UseSsl { get; set; }

Gets or sets whether to use SSL/TLS.

property UserName #

public string UserName { get; set; }

Gets or sets the RabbitMQ username.

property VirtualHost #

public string VirtualHost { get; set; }

Gets or sets the RabbitMQ virtual host.

class RabbitMqTransportPlugin

public sealed class RabbitMqTransportPlugin : IRouterTransportPlugin

Plugin for the client side of the RabbitMQ transport, which a microservice connects with. The server side is RabbitMqTransportServerPlugin in StellaOps.Router.Transport.RabbitMq.Server; both carry the transport name rabbitmq.

Members

MemberKindSummary
RabbitMqTransportPlugin()constructorCreates an instance of RabbitMqTransportPlugin.
DisplayNamepropertyGets the human-readable display name for this transport.
ModepropertyGets the side of the transport this plugin registers: RouterTransportMode.Client, RouterTransportMode.Server, or RouterTransportMode.Both.
TransportNamepropertyGets the transport name (e.g., “tcp”, “tls”, “udp”, “rabbitmq”, “inmemory”, “messaging”), shared by the client and the server plugin of one transport.
Register(RouterTransportRegistrationContext)methodRegisters transport services into the DI container for the side the context asks for.
RegisterShared(RouterTransportRegistrationContext)methodThe registration the client and the server plugin of the RabbitMQ transport share: frame envelope protection and the RabbitMqTransportOptionsbinding of the context’s configuration section (Router:Transport:RabbitMq when the context names none).

constructor RabbitMqTransportPlugin() #

public RabbitMqTransportPlugin()

Creates an instance of RabbitMqTransportPlugin.

property DisplayName #

public string DisplayName { get; }

Gets the human-readable display name for this transport.

property Mode #

public RouterTransportMode Mode { get; }

Gets the side of the transport this plugin registers: RouterTransportMode.Client, RouterTransportMode.Server, or RouterTransportMode.Both. IRouterTransportPlugin.Register(RouterTransportRegistrationContext)refuses a context that asks for a side outside this value (RouterTransportRegistrationContext.EnsureSupportedBy(IRouterTransportPlugin)).

property TransportName #

public string TransportName { get; }

Gets the transport name (e.g., “tcp”, “tls”, “udp”, “rabbitmq”, “inmemory”, “messaging”), shared by the client and the server plugin of one transport.

method Register(RouterTransportRegistrationContext) #

public void Register(RouterTransportRegistrationContext context)

Registers transport services into the DI container for the side the context asks for.

Parameters

  • context: The registration context containing services, configuration, and transport mode.

Exceptions

  • InvalidOperationException: The context asks for a side this plugin does not provide.

method RegisterShared(RouterTransportRegistrationContext) #

public static void RegisterShared(RouterTransportRegistrationContext context)

The registration the client and the server plugin of the RabbitMQ transport share: frame envelope protection and the RabbitMqTransportOptionsbinding of the context’s configuration section (Router:Transport:RabbitMq when the context names none). Both plugins call it from RabbitMqTransportPlugin.Register(RouterTransportRegistrationContext); a plugin for either side of the transport calls it the same way.

Parameters

  • context: The registration context of the plugin.