Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Orbis25/FoundationKit/llms.txt
Use this file to discover all available pages before exploring further.
FoundationKit.Events provides a first-class RabbitMQ event bus built on top of the official RabbitMQ.Client library. It covers the full publish/subscribe lifecycle: a fluent DI setup that connects to the broker at startup, an IRabbitMessageBroker service for publishing typed messages, an IMessageHandler<T> interface for consuming them, and an automatic background service that wires consumers to their queues. All message types, exchange topology, and handler registrations are discovered at startup with zero boilerplate.
EventExtensions
Located inFoundationKit.Events.Extensions, this static class exposes the two IServiceCollection extension methods used to configure the event bus.
AddEvents
Connects to RabbitMQ, declares the default exchange, registers the broker and topology infrastructure, auto-discovers allIMessageHandler<T> implementations in every loaded assembly, and starts the consumer background service.
IServiceCollection (fluent).
The application’s service collection, typically
builder.Services.Connection details and exchange/queue settings. See RabbitConfig for all fields.
AddEvents:
- Creates a
ConnectionFactory(usingUrlif set, otherwiseHost,User,Password,Port). - Opens an
IConnectionand anIChanneland registers both as singletons. - Registers
RabbitConfigandRabbitTopologyRegistryas singletons. - Registers
IRabbitMessageBroker→RabbitMessageBrokeras scoped. - Calls
ExchangeDeclareAsyncwithDefaultExchangeandDefaultExchangeType. - Scans all loaded assemblies (including those listed in the runtime dependency context) for concrete classes that implement a closed generic of
IMessageHandler<>, and registers each one as scoped. - Starts
RabbitMqConsumerServiceas a hosted background service.
AddSubscriber<T>
Registers aQueueDefinition that binds the queue for message type T to an exchange. Call this once per message type your application consumes, after AddEvents.
IServiceCollection (fluent).
The application’s service collection.
The exchange to bind to. When
null or empty, RabbitConfig.DefaultExchange is used.The message type to subscribe to. The resulting queue name is
"{QueuePrefix}:{typeof(T).Name}" — for example, "my-app:OrderCreated".AddSubscriber registers each QueueDefinition as a singleton using a factory delegate that resolves RabbitConfig from the container, so AddEvents (which registers RabbitConfig) must be called first.RabbitConfig
Carries all connection and topology settings passed toAddEvents.
RabbitMQ username. Ignored when
Url is set.RabbitMQ password. Ignored when
Url is set.RabbitMQ host name or IP address. Ignored when
Url is set.AMQP port.
0 means use the default port. Ignored when Url is set.Full AMQP URI (e.g.
"amqps://user:pass@host:5671/vhost"). When non-empty, takes priority over Host, User, Password, and Port.Name of the exchange that
AddEvents will declare and that IRabbitMessageBroker will publish to by default. Must not be null or empty.Exchange type used when declaring
DefaultExchange. Supported values:| Value | Wire name |
|---|---|
ExchangeType.Direct | "direct" |
ExchangeType.Fanout | "fanout" |
ExchangeType.Topic | "topic" |
ExchangeType.Headers | "headers" |
Whether un-acknowledged messages are requeued and redelivered by the consumer background service after a processing failure.
A namespace prefix applied to all queue names, formatted as
"{QueuePrefix}:{MessageTypeName}". Also prepended to routing keys by the publisher. Must not be null or empty.IRabbitMessageBroker
The primary publishing interface. InjectIRabbitMessageBroker into your services or controllers to publish messages to the RabbitMQ exchange. It is registered as scoped, so it is safe to inject into scoped or transient services.
PublishAsync (single message)
Publishes one message to the default or a specified exchange.The message to publish. Must implement
IMessage. The publisher wraps it in an EventMessage<TMessage> envelope before serialization.Override the exchange. When
null or empty, RabbitConfig.DefaultExchange is used.Override the routing key. When
null or empty, defaults to "{QueuePrefix}:{typeof(TMessage).Name}" (the same value used to name subscriber queues).Propagated to the underlying
IChannel.BasicPublishAsync call.CorrelationId— a new GUID per batch (shared across messages inPublishAsync(IEnumerable<...>)).MessageId— a new GUID per individual message.Type—typeof(TMessage).AssemblyQualifiedName.Persistent = true,ContentType = "application/json",ContentEncoding = "utf-8".
PublishAsync (batch)
Publishes a collection of messages of the same type, sequentially calling the single-message overload for each item.The messages to publish. Each is published as an independent AMQP message with its own
MessageId.Exchange override applied to every message in the batch.
Routing key override applied to every message in the batch.
Propagated to each individual publish call.
IMessage and IMessageHandler<T>
IMessage
A marker interface with no members. Implement it on any class or record to designate it as a message type that can be published and consumed through the event bus.IMessageHandler<TMessage>
Implement this interface to create a consumer for a specific message type. At startup,AddEvents scans all loaded assemblies and registers every concrete implementation as a scoped DI service. The RabbitMqConsumerService background service dispatches incoming messages to the appropriate handler.
The message type this handler processes. Must implement
IMessage.The deserialized message payload extracted from the
EventMessage<TMessage> envelope.Passed from the background consumer; respect it to support graceful shutdown.
Handler discovery is performed over all assemblies in the runtime dependency context, not just the entry-point assembly. You do not need to pass an
Assembly argument — simply ensure the assembly containing your handlers is referenced by (or is) the running application.EventMessage<T>
The envelope type that wraps every outgoing message. The publisher creates anEventMessage<TMessage> automatically — you never need to instantiate it directly. On the consumer side, the background service unwraps the envelope and delivers only the inner Data to HandleAsync.
A unique GUID string generated by the publisher for each individual message. Also set as the AMQP
MessageId property.The simple type name of
T (e.g. "OrderCreated"). Used as the default routing key suffix by the publisher.The UTC timestamp when the envelope was constructed by the publisher.
Correlation and timing metadata. See below.
The original message payload. The consumer background service extracts this and passes it to
IMessageHandler<T>.HandleAsync.EventMetadata
Arecord carrying correlation context that is automatically populated by RabbitMessageBroker.
A GUID string that correlates a group of related messages (shared within a single
PublishAsync call). Also set as the AMQP CorrelationId property.UTC timestamp recorded by
GetMetadata() at the moment of publishing — useful for latency tracking.