MiniTransit is a lightweight, extensible .NET messaging library designed as a simpler alternative to MassTransit and NServiceBus. It provides a minimal, easy-to-understand API for message-based communication in distributed systems, with a focus on flexibility and customizability.
- Simple Core API – Minimal abstractions for easy onboarding and maintenance.
- Pluggable Transports – Built-in support for In-Memory, RabbitMQ, and Azure Service Bus. Add your own by implementing
IMessageBus. - Automatic Consumer Registration – Register consumers via the builder for automatic subscription and background processing.
- Configurable Retry Policies – Built-in support for various retry strategies.
- Custom Serialization – Swap out the default JSON serializer for your own.
- .NET Standard – Works with modern .NET applications and libraries.
Add the core package and any transport you need:
dotnet add package MiniTransit
dotnet add package MiniTransit.RabbitMQ # Optional: RabbitMQ support
dotnet add package MiniTransit.AzureServiceBus # Optional: Azure Service Bus supportUse the provided extension methods to register MiniTransit and select a transport. Configuration is done via a builder pattern:
services.AddMiniTransit((settings, builder) =>
{
builder.UseInMemory();
// Or for RabbitMQ:
// builder.UseRabbitMQ(options => { options.HostName = "localhost"; ... });
// Or for Azure Service Bus:
// builder.UseAzureServiceBus(options => { options.ConnectionString = "..."; ... });
});You can register consumers for automatic background subscription and processing:
services.AddMiniTransit((settings, builder) =>
{
builder.UseInMemory();
builder.AddConsumer<MyConsumer>();
builder.AddConsumer<AnotherConsumer>();
});When using AddConsumer<T>(), MiniTransit will automatically subscribe these consumers to the appropriate message types and run them as hosted services.
All consumers from an assembly can be registered automatically as well:
services.AddMiniTransit((settings, builder) =>
{
builder.UseInMemory();
builder.AddConsumers(typeof(MyConsumer).Assembly);
});public class MyConsumer : IConsumer<MyMessage>
{
public Task ConsumeAsync(ConsumeContext<MyMessage> context)
{
// Handle the message
return Task.CompletedTask;
}
}If you prefer manual subscription, you can still resolve IBus and subscribe:
var bus = serviceProvider.GetRequiredService<IBus>();
await bus.SubscribeAsync<MyMessage, MyConsumer>();
await bus.StartProcessingAsync();
await bus.PublishAsync(new MyMessage { ... });await bus.ScheduleAsync(new MyMessage { ... }, TimeSpan.FromSeconds(10));MiniTransit supports flexible retry policies for message processing failures. Configure retry policies using the builder’s UseRetry method:
services.AddMiniTransit((settings, builder) =>
{
builder.UseInMemory();
builder.UseRetry(retry =>
{
retry.Immediate(3); // 3 immediate retries
retry.Ignore<ArgumentException>(); // Ignore specific exceptions
});
});Supported retry strategies include:
- Immediate: Retry immediately a specified number of times.
- Interval: Retry with a fixed interval.
- Custom Intervals: Retry with custom intervals.
- Exponential Backoff: Retry with exponentially increasing intervals.
- Custom Policy: Use your own implementation.
You can replace the default JSON serializer with your own implementation:
builder.UseMessageSerializer<MyCustomSerializer>();The following transports are currently supported:
An in-memory implementation using .NET Channels for asynchronous messaging and scheduling.
The RabbitMQ transport in MiniTransit provides robust messaging capabilities using RabbitMQ's topic exchanges and queues. It supports advanced features for message processing, retry policies, and scheduling.
- Durable Queues: Option to create queues as durable for persistence across RabbitMQ restarts.
- Dead Letter Queues: Automatically route failed messages to a Dead Letter Queue for further inspection.
- Retry Policies: Configurable retry mechanism with support for retry counts and delayed retries.
- Message Scheduling: Leverages the
rabbitmq_delayed_message_exchangeplugin for delayed message delivery. - TLS Support: Secure communication with RabbitMQ using TLS.
- Prefetch Count: Control the number of messages fetched from RabbitMQ in one go.
- Auto Acknowledgment: Automatically acknowledge messages upon receipt or process them manually.
- Customizable Time-to-Live: Set a default expiration time for messages.
- Message Routing: Messages are routed using RabbitMQ's topic exchanges. The routing key is derived from the message type name, ensuring that messages are delivered to all subscribed consumers.
- Queue Naming: Unique queue names are generated based on the message type and consumer type, ensuring isolation between different consumers.
- Error Handling: Failed messages are retried based on the configured retry policy. If retries are exhausted, messages can be sent to a Dead Letter Queue (if enabled).
- Scheduling: Messages can be scheduled for future delivery using the RabbitMQ delayed message exchange plugin.
services.AddMiniTransit((settings, builder) =>
{
builder.UseRabbitMQ(options =>
{
options.HostName = "localhost";
options.Port = 5672;
options.UserName = "guest";
options.Password = "guest";
options.DeadLetterOnError = true;
options.DefaultMessageTimeToLiveDays = 365;
// ...other settings
});
});The Azure Service Bus transport in MiniTransit provides robust messaging capabilities using Azure's fully managed message broker. It supports advanced features for message processing, retry policies, and subscription management.
- Automatic Subscription Creation: Automatically create topics and subscriptions if they do not exist.
- Dead Letter Queues: Route failed messages to a Dead Letter Queue for further inspection.
- Retry Policies: Configurable retry mechanism with support for retry counts and delayed retries.
- Message Scheduling: Schedule messages for future delivery.
- Subject Filtering: Filter messages to subscriptions based on the message type.
- Lock Management: Configurable lock duration and automatic lock renewal.
- Concurrent Processing: Control the number of concurrent message handlers.
- Message Routing: Messages are routed to topics and subscriptions. Subject filtering can be enabled to route messages based on their type.
- Subscription Management: Subscriptions can be created automatically if they do not exist, with configurable settings such as message TTL, lock duration, and max delivery count. The provided connection string must have the 'Manage' claim to allow subscriptions to be managed dynamically.
- Error Handling: Failed messages are retried based on the configured retry policy. If retries are exhausted, messages can be sent to a Dead Letter Queue (if enabled).
- Scheduling: Messages can be scheduled for future delivery using Azure Service Bus's built-in scheduling capabilities.
services.AddMiniTransit((settings, builder) =>
{
builder.UseAzureServiceBus(options =>
{
options.ConnectionString = "<your-connection-string>";
options.CreateSubscriptions = true;
options.MaxDeliveryCount = 3;
// ...other settings
});
});Each transport has its own settings class for advanced configuration. See the Settings folder in each transport project for details.
To add a new transport, implement the IMessageBus interface and provide an extension method for registration. See the MiniTransit.RabbitMQ and MiniTransit.AzureServiceBus projects for examples.
Contributions are welcome! Please open issues or submit pull requests.
MIT