StellaOps.Router.Transport.RabbitMq
Router RabbitMQ transport, client side: connects a microservice to a gateway through RabbitMQ queues.
| Package | StellaOps.Router.Transport.RabbitMq |
| Component | Router |
| Licence | Apache-2.0 |
| Package kind | Library |
| Target framework | net10.0 |
To add the package to a project, see Installing Stella Ops packages.
Namespaces
| Namespace | Types |
|---|---|
StellaOps.Router.Transport.RabbitMq | 5 |
StellaOps.Router.Transport.RabbitMq namespace
| Type | Kind | Description |
|---|---|---|
RabbitMqFrameProtocol | class | Handles serialization and deserialization of frames for RabbitMQ transport. |
RabbitMqTransportClient | class | RabbitMQ transport client implementation for microservices. |
RabbitMqTransportClientServiceCollectionExtensions | class | Extension methods for registering RabbitMQ transport services. |
RabbitMqTransportOptions | class | Options for RabbitMQ transport configuration. |
RabbitMqTransportPlugin | class | Plugin for the client side of the RabbitMQ transport, which a microservice connects with. |
RabbitMqFrameProtocol class
public static class RabbitMqFrameProtocol
Handles serialization and deserialization of frames for RabbitMQ transport.
CreateProperties(Frame, string, TimeSpan?) method
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.
ExtractConnectionId(IReadOnlyBasicProperties) method
public static string ExtractConnectionId(IReadOnlyBasicProperties properties)
Extracts the connection ID from message properties.
Parameters
properties: The message properties.
Returns
The connection ID.
ParseFrame(ReadOnlyMemory<byte>, IReadOnlyBasicProperties) method
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 theReceivedAsynchandler returns, and frames are read later.properties: The message properties.
Returns
The parsed frame.
RabbitMqTransportClient class
public sealed class RabbitMqTransportClient : ITransportClient, IMicroserviceTransport, IAsyncDisposable
RabbitMQ transport client implementation for microservices.
RabbitMqTransportClient(IOptions<RabbitMqTransportOptions>, ILogger<RabbitMqTransportClient>, IFrameEnvelopeSigner, IFrameEnvelopeVerifier) constructor
public RabbitMqTransportClient(IOptions<RabbitMqTransportOptions> options, ILogger<RabbitMqTransportClient> logger, IFrameEnvelopeSigner? envelopeSigner = null, IFrameEnvelopeVerifier? envelopeVerifier = null)
Initializes a new instance of the RabbitMqTransportClientclass.
OnCancelReceived event
public event Func<Guid, string?, Task>? OnCancelReceived
Event raised when a CANCEL frame is received.
OnRequestReceived event
public event Func<Frame, CancellationToken, Task<Frame>>? OnRequestReceived
Event raised when a REQUEST frame is received.
CancelAllInflight(string) method
public void CancelAllInflight(string reason)
Cancels all in-flight handlers.
ConnectAsync(InstanceDescriptor, IReadOnlyList<EndpointDescriptor>, CancellationToken) method
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.
ConnectAsync(InstanceDescriptor, IReadOnlyList<EndpointDescriptor>, IReadOnlyDictionary<string, SchemaDefinition>, ServiceOpenApiInfo, CancellationToken) method
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.
DisconnectAsync() method
public Task DisconnectAsync()
Disconnects from the gateway.
DisposeAsync() method
public ValueTask DisposeAsync()
See IAsyncDisposable.DisposeAsync().
SendCancelAsync(ConnectionState, Guid, string) method
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.
SendHeartbeatAsync(HeartbeatPayload, CancellationToken) method
public Task SendHeartbeatAsync(HeartbeatPayload heartbeat, CancellationToken cancellationToken)
Sends a heartbeat.
SendRequestAsync(ConnectionState, Frame, TimeSpan, CancellationToken) method
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.
SendStreamingAsync(ConnectionState, Frame, Stream, Func<ResponseFrame, Stream, Task>, PayloadLimits, CancellationToken) method
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; itsResponseFrame.Payloadis empty) and the response body stream.limits: Payload limits to enforce.cancellationToken: Cancellation token.
RabbitMqTransportClientServiceCollectionExtensions class
public static class RabbitMqTransportClientServiceCollectionExtensions
Extension methods for registering RabbitMQ transport services.
AddRabbitMqTransportClient(IServiceCollection, Action<RabbitMqTransportOptions>) extension method
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.
RabbitMqTransportOptions class
public sealed class RabbitMqTransportOptions
Options for RabbitMQ transport configuration.
RabbitMqTransportOptions() constructor
public RabbitMqTransportOptions()
Creates an instance of RabbitMqTransportOptions.
AutoDeleteQueues property
public bool AutoDeleteQueues { get; set; }
Gets or sets whether queues should auto-delete on disconnect.
AutomaticRecoveryEnabled property
public bool AutomaticRecoveryEnabled { get; set; }
Gets or sets whether to use automatic recovery.
DefaultTimeout property
public TimeSpan DefaultTimeout { get; set; }
Gets or sets the default request timeout.
DurableQueues property
public bool DurableQueues { get; set; }
Gets or sets whether queues should be durable.
ExchangePrefix property
public string ExchangePrefix { get; set; }
Gets or sets the exchange prefix.
HostName property
public string HostName { get; set; }
Gets or sets the RabbitMQ host name.
InstanceId property
public string? InstanceId { get; set; }
Gets or sets the instance ID for this microservice instance.
MaxMessageSize property
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.
NetworkRecoveryInterval property
public TimeSpan NetworkRecoveryInterval { get; set; }
Gets or sets the network recovery interval.
NodeId property
public string? NodeId { get; set; }
Gets or sets the node ID for this gateway instance.
Password property
public string Password { get; set; }
Gets or sets the RabbitMQ password.
Port property
public int Port { get; set; }
Gets or sets the RabbitMQ port.
PrefetchCount property
public ushort PrefetchCount { get; set; }
Gets or sets the prefetch count (concurrent messages).
QueuePrefix property
public string QueuePrefix { get; set; }
Gets or sets the queue prefix.
RequestExchange property
public string RequestExchange { get; }
Gets or sets the request exchange name.
ResponseExchange property
public string ResponseExchange { get; }
Gets or sets the response exchange name.
SslCertPath property
public string? SslCertPath { get; set; }
Gets or sets the SSL certificate path.
UseSsl property
public bool UseSsl { get; set; }
Gets or sets whether to use SSL/TLS.
UserName property
public string UserName { get; set; }
Gets or sets the RabbitMQ username.
VirtualHost property
public string VirtualHost { get; set; }
Gets or sets the RabbitMQ virtual host.
RabbitMqTransportPlugin class
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.
RabbitMqTransportPlugin() constructor
public RabbitMqTransportPlugin()
Creates an instance of RabbitMqTransportPlugin.
DisplayName property
public string DisplayName { get; }
Gets the human-readable display name for this transport.
Mode property
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)).
TransportName property
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.
Register(RouterTransportRegistrationContext) method
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.
RegisterShared(RouterTransportRegistrationContext) method
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.
