MVFC.Outbox.MongoDB
2.0.1
See the version list below for details.
dotnet add package MVFC.Outbox.MongoDB --version 2.0.1
NuGet\Install-Package MVFC.Outbox.MongoDB -Version 2.0.1
<PackageReference Include="MVFC.Outbox.MongoDB" Version="2.0.1" />
<PackageVersion Include="MVFC.Outbox.MongoDB" Version="2.0.1" />
<PackageReference Include="MVFC.Outbox.MongoDB" />
paket add MVFC.Outbox.MongoDB --version 2.0.1
#r "nuget: MVFC.Outbox.MongoDB, 2.0.1"
#:package MVFC.Outbox.MongoDB@2.0.1
#addin nuget:?package=MVFC.Outbox.MongoDB&version=2.0.1
#tool nuget:?package=MVFC.Outbox.MongoDB&version=2.0.1
MVFC.Outbox.MongoDB
Stores Outbox messages using the native MongoDB Driver. Supports transactional batches, atomic message reservations with find-and-update, and TTL indexes.
Supports .NET 10 and .NET 11.
dotnet add package MVFC.Outbox.MongoDB
Configuration and usage
The examples use the application's services and variables: services, configuration, connection, ct, order and topicId. Register services before building the host.
Resolve IMongoClient, IOutboxPublisher and MongoOutboxSession from the same dependency injection (DI) scope.
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
using MVFC.Outbox.DependencyInjection;
using MVFC.Outbox.MongoDB;
using MVFC.Outbox.Publishing;
services.AddOutbox(configuration, outbox => outbox.ProducerOnly()
.MapAggregate<Order, Guid>(order => order.Id, "order")
.UseMongoDB(mongo =>
{
mongo.ConnectionString = connection;
mongo.DatabaseName = "orders";
mongo.CollectionName = "outbox";
}));
// Resolve these services from the SAME DI scope.
var client = scope.ServiceProvider.GetRequiredService<IMongoClient>();
var publisher = scope.ServiceProvider.GetRequiredService<IOutboxPublisher>();
var outboxSession = scope.ServiceProvider.GetRequiredService<MongoOutboxSession>();
using var transaction = await client.StartSessionAsync(cancellationToken: ct);
transaction.StartTransaction();
outboxSession.Enlist(transaction);
await client.GetDatabase("orders").GetCollection<Order>("orders")
.InsertOneAsync(transaction, order, cancellationToken: ct);
var receipt = await publisher.For(order).To(topicId).EnqueueAsync(ct); // WriteState = Staged
await transaction.CommitTransactionAsync(ct);
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
public sealed class Order
{
[BsonGuidRepresentation(GuidRepresentation.Standard)]
public Guid Id { get; set; }
public decimal Total { get; set; }
}
Transactions and concurrency
External session (
MongoOutboxSession): create the session with the sameIMongoClientused by the Outbox and associate it with the same dependency injection (DI) scope.EnqueueAsyncreturnsStaged. Aborting the transaction rolls back the messages.Keep the associated session active for later enqueue calls in that scope.
No external session:
EnqueueAsyncsaves the entire batch in its own MongoDB transaction and returnsPersisted. This transaction does not include separate business writes.Requirements: MongoDB transactions require a replica set or sharded cluster, and collections must exist before starting the transaction.
Leases: claims use atomic
findOneAndUpdate, preventing competing workers from reserving the same message.
Schema initialization, TTL and existing collections
When AddOutbox(...UseMongoDB...) is registered, resolve IOutboxSchemaInitializer directly. To initialize the schema in a separate step, without configuring topics or starting a worker, use AddMongoDBOutboxSchema:
using MVFC.Outbox.MongoDB;
using MVFC.Outbox.Persistence;
services.AddMongoDBOutboxSchema(mongo =>
{
mongo.ConnectionString = connection;
mongo.DatabaseName = "orders";
mongo.CollectionName = "outbox";
});
await using var scope = provider.CreateAsyncScope();
await scope.ServiceProvider.GetRequiredService<IOutboxSchemaInitializer>().InitializeAsync(ct);
Indexes & TTL:
InitializeAsynccreates the collection and indexes for dispatch, aggregate queries and lease expiration. It also creates a single-field TTL index onExpiresAtUtcwithexpireAfterSeconds: 0. OnlyProcessedrecords receiveExpiresAtUtc.MongoDB's TTL monitor removes expired records asynchronously.
Existing collections: documents without
ProjectIdorAttributesJsonremain readable (default project routing, no custom attributes). If migrating older documents that lackDestinationKind(Transport = 0,Handler = 1), fill in the missing field before deployment:
db.outbox.updateMany(
{ DestinationKind: { $exists: false } },
{ $set: { DestinationKind: 0 } }
);
Requirements
An application targeting .NET 10 or .NET 11.
Access to MongoDB through MongoDB.Driver. Transactional publication requires a replica set or sharded cluster; a standalone server does not support these transactions.
For development, the playground and integration tests, use the .NET 11 SDK specified in global.json.
To build the packages, also install .NET 10 targeting support. Keep Docker running. See the playground guide.
Contributing
See CONTRIBUTING.md to set up the environment, run the Cake build and follow the test and coverage conventions. Dependency versions are defined in Directory.Packages.props.
License
Distributed under the Apache License 2.0. See LICENSE for all terms: permissions, redistribution conditions, and limits on warranty and liability.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
-
net10.0
- Microsoft.Extensions.Hosting (>= 10.0.15)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.15)
- MongoDB.Driver (>= 3.14.1)
- MVFC.Outbox (>= 2.0.1)
-
net11.0
- Microsoft.Extensions.Hosting (>= 11.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 11.0.0)
- MongoDB.Driver (>= 3.14.1)
- MVFC.Outbox (>= 2.0.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on MVFC.Outbox.MongoDB:
| Package | Downloads |
|---|---|
|
MVFC.Outbox.MongoDB.EntityFrameworkCore
Transactional Outbox staging through EF Core, with native MongoDB worker operations and schema provisioning. |
GitHub repositories
This package is not used by any popular GitHub repositories.