Storage.MongoDb - dotnet-shashlik/shashlik.eventbus GitHub Wiki
MongoDB 存储
NuGet:Shashlik.EventBus.MongoDb
注册方式
方式一:直接指定连接字符串
services.AddEventBus()
.AddMongoDb("mongodb://localhost", publishCollectionName: "eventbus_published", receiveCollectionName: "eventbus_received");
方式二:通过配置文件
services.AddEventBus()
.AddMongoDb(configuration.GetSection("MongoDb"));
配置文件示例:
MongoDb:
ConnectionString: "mongodb://localhost"
DataBase: "eventbus"
PublishedCollectionName: "eventbus_published"
ReceivedCollectionName: "eventbus_received"
方式三:自定义配置
services.AddEventBus()
.AddMongoDb(options =>
{
options.ConnectionString = "mongodb://localhost";
options.DataBase = "eventbus";
});
配置项 EventBusMongoDbOptions
| 配置项 | 默认值 | 说明 |
|---|---|---|
PublishedCollectionName |
eventbus_published |
已发布消息集合名称 |
ReceivedCollectionName |
eventbus_received |
已接收消息集合名称 |
DataBase |
eventbus |
数据库名称 |
ConnectionString |
mongodb://localhost |
连接字符串 |
事务上下文
MongoDB 事务需要集群支持。事务上下文获取方式:
// 方式一:扩展方法
var txContext = clientSessionHandle.GetTransactionContext();
// 方式二:手动构造
var txContext = new MongoDbTransactionContext(clientSessionHandle);
await EventPublisher.PublishAsync(newEvent, txContext);