MVFC.Outbox.MongoDB 4.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MVFC.Outbox.MongoDB --version 4.1.0
                    
NuGet\Install-Package MVFC.Outbox.MongoDB -Version 4.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MVFC.Outbox.MongoDB" Version="4.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MVFC.Outbox.MongoDB" Version="4.1.0" />
                    
Directory.Packages.props
<PackageReference Include="MVFC.Outbox.MongoDB" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MVFC.Outbox.MongoDB --version 4.1.0
                    
#r "nuget: MVFC.Outbox.MongoDB, 4.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MVFC.Outbox.MongoDB@4.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MVFC.Outbox.MongoDB&version=4.1.0
                    
Install as a Cake Addin
#tool nuget:?package=MVFC.Outbox.MongoDB&version=4.1.0
                    
Install as a Cake Tool

MVFC.Outbox.MongoDB

🇧🇷 Leia em Português

CI codecov License Platform NuGet Version NuGet Downloads

Main guide · Playground

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

ProducerOnly saves messages; ConsumerProducer also starts the worker. Manual dispatch uses the same confirmation flow (IOutboxExecutionPersistence). For custom registration updates, see the 4.0.0 migration guide.

Configuration and usage

These example variables come from your application: 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 MVFC.Outbox.Extensions;
using MVFC.Outbox.MongoDB;
using MVFC.Outbox.MongoDB.Extensions;
using MVFC.Outbox.Publishing;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;

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 same IMongoClient used by the Outbox and associate it with the same dependency injection (DI) scope. EnqueueAsync returns Staged. Aborting the transaction rolls back the messages.

    Keep the associated session active for later enqueue calls in that scope.

  • No external session: EnqueueAsync saves the entire batch in its own MongoDB transaction and returns Persisted. 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.MongoDB.Extensions;
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: InitializeAsync creates the collection and indexes for dispatch, aggregate queries and lease expiration. It also creates a single-field TTL index on ExpiresAtUtc with expireAfterSeconds: 0. Only Processed records receive ExpiresAtUtc.

    MongoDB's TTL monitor removes expired records asynchronously.

  • Existing collections: documents without ProjectId or AttributesJson remain readable (default project routing, no custom attributes). If migrating older documents that lack DestinationKind (Transport = 0, Handler = 1), fill in the missing field before deployment:

db.outbox.updateMany(
  { DestinationKind: { $exists: false } },
  { $set: { DestinationKind: 0 } }
);

Ordered acquisition indexes

FindOneAndUpdate reserves each message atomically, generates a token and increments Version. Renewal, completion and failure require the current token and a valid lease. Date and lease filters remain unchanged.

Before starting workers, run IOutboxSchemaInitializer.InitializeAsync. It keeps existing indexes, including TTL, and adds:

Index Ordering
ix_outbox_ready Status, priority, queue age and ID for new messages.
ix_outbox_retry The same keys, with the status direction used for retries.

IsTerminal follows the sort keys. Its filter accepts values other than true, including a missing field; placing it first would prevent using the index ordering.

Validate plans with realistic delayed messages and active leases: indexes do not guarantee linear scaling. No new MongoDB 8 requirement or application lock is introduced.

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, use the .NET 11 SDK in global.json and .NET 10 targeting support. The playground and integration tests also require Docker. See the playground guide.

Contributing

See CONTRIBUTING.md for setup, Cake builds, tests and coverage conventions. Dependencies are listed 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last Updated
6.0.0 0 12/4/2026
5.0.0 53 12/3/2026
4.1.0 58 12/2/2026
4.0.0 89 12/1/2026
3.0.0 85 12/1/2026
2.0.1 93 11/30/2026
2.0.0 90 11/30/2026
1.1.1 98 11/30/2026
1.1.0 94 11/30/2026
1.0.1 97 11/30/2026
1.0.0 93 11/30/2026