zarusz/SlimMessageBus
关于
面向 .NET 的轻量级消息总线接口(发布/订阅和请求-响应),并为常用消息代理提供传输插件。
系统与基础设施可以做什么
- Supports multiple messaging patterns: pub/sub, request-response, queues
- Compatible with popular brokers: Kafka, RabbitMQ, Azure EventHubs, MQTT, Redis, and more
- Fluent API for easy configuration
- Plugin architecture for serialization, validation, outbox patterns and interceptor pipeline
实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- -1
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文SlimMessageBus
SlimMessageBus is a lightweight, flexible, and extensible messaging framework for .NET, supporting multiple message brokers, including Kafka, RabbitMQ, Azure EventHubs, MQTT, Redis Pub/Sub, and more. It simplifies asynchronous communication and integrates seamlessly with modern .NET applications.
See how to migrate from MediatR or MassTransit
🚀 Quick Start
Installation
dotnet add package SlimMessageBus
# Add specific transport provider, e.g. Kafka
dotnet add package SlimMessageBus.Host.Kafka
# Add serialization plugin
dotnet add package SlimMessageBus.Host.Serialization.SystemTextJson
Basic Usage
Publishing Messages
IMessageBus bus; // injected
public record OrderCreatedEvent(int OrderId);
await bus.Publish(new OrderCreatedEvent(123));
Consuming Messages
public class OrderCreatedEventConsumer : IConsumer<OrderCreatedEvent>
{
public async Task OnHandle(OrderCreatedEvent message, CancellationToken cancellationToken)
{
// Handle the event
}
}
Request-Response Example
Sending a Request
public record CreateCustomerCommand(string Name) : IRequest<CreateCustomerCommandResult>;
public record CreateCustomerCommandResult(Guid CustomerId);
var result = await bus.Send(new CreateCustomerCommand("John Doe"));
Handling a Request
public class CreateCustomerCommandHandler : IRequestHandler<CreateCustomerCommand, CreateCustomerCommandResult>
{
public async Task<CreateCustomerCommandResult> OnHandle(CreateCustomerCommand request, CancellationToken cancellationToken)
{
// Create customer logic
return new(Guid.NewGuid());
}
}
Configuration Example
services.AddSlimMessageBus(mbb =>
{
mbb.AddChildBus("Bus1", builder =>
{
builder
// the pub-sub events
.Produce<OrderCreatedEvent>(x => x.DefaultPath("orders-topic"))
.Consume<OrderCreatedEvent>(x => x.Path("orders-topic")
//.WithConsumer<OrderCreatedEventConsumer>() // Optional: can be skipped as IConsumer<OrderCreatedEvent> will be resolved from DI
//.KafkaGroup("kafka-consumer-group") // Kafka: Consumer Group
//.SubscriptionName("azure-sb-topic-subscription") // Azure ServiceBus: Subscription Name
)
// the request-response
.Produce<CreateCustomerCommand>(x => x.DefaultPath("customer-requests"))
.Handle<CreateCustomerCommand, CreateCustomerCommandResult>(x => x.Path("customer-requests"))
// Use Kafka transport provider (requires SlimMessageBus.Host.Kafka package)
.WithProviderKafka(cfg => cfg.BrokerList = "localhost:9092");
// Use Azure Service Bus transport provider
//.WithProviderServiceBus(cfg => { ... }) // requires SlimMessageBus.Host.AzureServiceBus package
// Use Azure Event Hub transport provider
//.WithProviderEventHub(cfg => { ... }) // requires SlimMessageBus.Host.AzureEventHub package
// Use Redis transport provider
//.WithProviderRedis(cfg => { ... }) // requires SlimMessageBus.Host.Redis package
// Use RabbitMQ transport provider
//.WithProviderRabbitMQ(cfg => { ... }) // requires SlimMessageBus.Host.RabbitMQ package
// Use in-memory transport provider
//.WithProviderMemory(cfg => { ... }) // requires SlimMessageBus.Host.Memory package
})
// Add other bus transports (as child bus for in memory domain events), if needed
//.AddChildBus("Bus2", (builder) => { })
.AddJsonSerializer() // requires SlimMessageBus.Host.Serialization.SystemTextJson or SlimMessageBus.Host.Serialization.Json package
.AddServicesFromAssemblyContaining<OrderCreatedEventConsumer>();
});
The configuration can be modularized (for modular monoliths).
📖 Documentation
- Introduction
- Agent Skills
- Transports:
- Plugins:
- Samples
- Use Cases
📦 NuGet Packages
| Name | Description | NuGet |
|---|---|---|
SlimMessageBus |
The core API for SlimMessageBus | |
| Transports | ||
.Host.AmazonSQS |
Transport provider for Amazon SQS / SNS | |
.Host.AzureEventHub |
Transport provider for Azure Event Hubs | |
.Host.AzureServiceBus |
Transport provider for Azure Service Bus | |
.Host.GooglePubSub |
Transport provider for Google Cloud Pub/Sub | |
.Host.Kafka |
Transport provider for Apache Kafka | |
.Host.MQTT |
Transport provider for MQTT | |
.Host.Memory |
Transport provider implementation for in-process (in memory) message passing (no messaging infrastructure required) | |
.Host.NATS |
Transport provider for NATS | |
.Host.PostgreSql |
Transport provider implementation for PostgreSQL database message passing | |
.Host.RabbitMQ |
Transport provider for RabbitMQ | |
.Host.Redis |
Transport provider for Redis | |
.Host.Sql |
Transport provider implementation for SQL Server / Azure SQL database message passing | |
| Serialization | ||
.Host.Serialization.Json |
Serialization plugin for JSON (Newtonsoft.Json library) | |
.Host.Serialization.SystemTextJson |
Serialization plugin for JSON (System.Text.Json library) | |
.Host.Serialization.Avro |
Serialization plugin for Avro (Apache.Avro library) | |
.Host.Serialization.Hybrid |
Plugin that delegates serialization to other serializers based on message type | |
.Host.Serialization.GoogleProtobuf |
Serialization plugin for Google Protobuf | |
| Plugins | ||
.Host.AspNetCore |
Integration for ASP.NET Core | |
.Host.Interceptor |
Core interface for interceptors | |
.Host.FluentValidation |
Validation for messages based on FluentValidation | |
.Host.Outbox.PostgreSql |
Transactional Outbox using PostgreSQL | |
.Host.Outbox.PostgreSql.DbContext |
Transactional Outbox using PostgreSQL with EF DataContext integration | |
.Host.Outbox.Sql |
Transactional Outbox using MSSQL | |
.Host.Outbox.Sql.DbContext |
Transactional Outbox using MSSQL with EF DataContext integration | |
.Host.Outbox.MongoDb (beta) |
Transactional Outbox using MongoDB | |
.Host.AsyncApi |
AsyncAPI specification generation via Saunter | |
.Host.CircuitBreaker.HealthCheck |
Consumer circuit breaker based on health checks |
Typically the application layers (domain model, business logic) only need to depend on SlimMessageBus which is the facade, and ultimately the application hosting layer (ASP.NET, Console App, Windows Service) will reference and configure the other packages (SlimMessageBus.Host.*) which are the messaging transport providers and additional plugins.
🎯 Features
- Supports multiple messaging patterns: pub/sub, request-response, queues
- Compatible with popular brokers: Kafka, RabbitMQ, Azure EventHubs, MQTT, Redis, and more
- Fluent API for easy configuration
- Plugin architecture for serialization, validation, outbox patterns and interceptor pipeline
- Integration with .NET dependency injection
- Modern async/await support
- Minimal external dependencies
- SourceLink support
- Because SlimMessageBus is a facade, chosen messaging transports can be swapped without impacting the overall application architecture.
🚧 Contributing
We welcome contributions! See CONTRIBUTING.md for details on submitting issues, feature requests, and pull requests. See here.
💬 Community
- Raise issues, discussions, questions and feature requests here.
📜 License
SlimMessageBus is licensed under the Apache License 2.0.
🙌 Credits
Special thanks to:
- Our maintainers
- Gravity9 for Azure infrastructure support.
- Redis Labs, CloudKarafka, HiveMQ, CloudAMQP for providing infrastructure for testing.
探索相关项目
加载中…