MVFC.Outbox.MongoDB.EntityFrameworkCore 4.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MVFC.Outbox.MongoDB.EntityFrameworkCore --version 4.0.0
                    
NuGet\Install-Package MVFC.Outbox.MongoDB.EntityFrameworkCore -Version 4.0.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.EntityFrameworkCore" Version="4.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MVFC.Outbox.MongoDB.EntityFrameworkCore" Version="4.0.0" />
                    
Directory.Packages.props
<PackageReference Include="MVFC.Outbox.MongoDB.EntityFrameworkCore" />
                    
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.EntityFrameworkCore --version 4.0.0
                    
#r "nuget: MVFC.Outbox.MongoDB.EntityFrameworkCore, 4.0.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.EntityFrameworkCore@4.0.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.EntityFrameworkCore&version=4.0.0
                    
Install as a Cake Addin
#tool nuget:?package=MVFC.Outbox.MongoDB.EntityFrameworkCore&version=4.0.0
                    
Install as a Cake Tool

MVFC.Outbox.MongoDB.EntityFrameworkCore

🇧🇷 Leia em Português

CI codecov License Platform NuGet Version NuGet Downloads

Main guide · Playground

Reads and writes MongoDB Outbox records through EF Core. Uses the native MongoDB driver to create the collection and indexes.

Supports .NET 10 and .NET 11. Both targets use EF Core 10 for compatibility with the MongoDB EF Core provider.

dotnet add package MVFC.Outbox.MongoDB.EntityFrameworkCore

This provider implements IOutboxExecutionPersistence: manual dispatch and ConsumerProducer use the same execution and confirmation flow. ProducerOnly enqueues messages without starting the worker. Built-in registration is unchanged; see the 4.0.0 migration guide for renamed public types and custom providers.

Configuration and usage

The examples use the application's services and variables: services, configuration, connection, ct, order and topicId. Register your DbContext with UseMongoDB and map model.AddOutbox().ToCollection("outbox") in OnModelCreating.

Use matching values for ConnectionString, DatabaseName and CollectionName in UseMongoDB and UseMongoDBEntityFramework.

using MVFC.Outbox.EntityFrameworkCore;
using MVFC.Outbox.EntityFrameworkCore.Extensions;
using MVFC.Outbox.Extensions;
using MVFC.Outbox.MongoDB.EntityFrameworkCore;
using MVFC.Outbox.MongoDB.EntityFrameworkCore.Extensions;
using MVFC.Outbox.MongoDB.Extensions;
using MVFC.Outbox.Publishing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.EntityFrameworkCore.Extensions;
using MongoDB.EntityFrameworkCore.Metadata;

services.AddDbContext<ApplicationDbContext>(db => db.UseMongoDB(connection, "orders"));
services.AddOutbox(configuration, outbox => outbox.ProducerOnly()
    .MapAggregate<Order, Guid>(order => order.Id, "order")
    .UseMongoDBEntityFramework<ApplicationDbContext>(mongo =>
    {
        mongo.ConnectionString = connection;
        mongo.DatabaseName = "orders";
        mongo.CollectionName = "outbox";
    }));

// Same scoped ApplicationDbContext used by the publisher:
await using var transaction = await database.Database.BeginTransactionAsync(ct);
database.Orders.Add(order);
await publisher.For(order).To(topicId).EnqueueAsync(ct);
await database.SaveChangesAsync(ct);
await transaction.CommitAsync(ct);

public sealed class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
    : DbContext(options)
{
    public DbSet<Order> Orders => Set<Order>();

    protected override void OnModelCreating(ModelBuilder model)
    {
        model.Entity<Order>().HasKey(order => order.Id);
        model.Entity<Order>().ToCollection("orders");
        model.AddOutbox().ToCollection("outbox");
    }
}

public sealed record Order(Guid Id, decimal Total);
  • Runtime & transactions: reads and writes use the shared EF Core adapter. The Version field detects conflicting updates through optimistic concurrency. EnqueueAsync returns Staged; call SaveChangesAsync and commit the transaction to save the messages.

    MongoDB transactions require a replica set or sharded cluster.

  • Without a context type: UseMongoDBEntityFramework(mongo => ...) creates a separate Outbox-only DbContext and does not share transactions with another business context.

Dedicated index initialization and existing collections

When AddOutbox(...UseMongoDBEntityFramework...) is registered, resolve IOutboxSchemaInitializer directly. To initialize the schema in a separate step, without configuring topics or starting a worker, use AddMongoDBEntityFrameworkOutboxSchema:

using MVFC.Outbox.MongoDB.EntityFrameworkCore;
using MVFC.Outbox.MongoDB.EntityFrameworkCore.Extensions;
using MVFC.Outbox.Persistence;

services.AddMongoDBEntityFrameworkOutboxSchema(mongo =>
{
    mongo.ConnectionString = connection;
    mongo.DatabaseName = "orders";
    mongo.CollectionName = "outbox";
});
await using var scope = provider.CreateAsyncScope();
await scope.ServiceProvider.GetRequiredService<IOutboxSchemaInitializer>().InitializeAsync(ct);
  • InitializeAsync uses the native MongoDB driver to create the Outbox collection and indexes, including the TTL index on ExpiresAtUtc with expireAfterSeconds: 0. Manage business collections separately.

  • Documents without ProjectId or AttributesJson remain readable. For documents created before DestinationKind (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.

  • A MongoDB EF Core DbContext with the Outbox mapping. Transactional publication requires MongoDB configured as a replica set or sharded cluster.

  • 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 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

This package is not used by any NuGet packages.

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 56 12/3/2026
4.1.0 72 12/2/2026
4.0.0 76 12/1/2026
3.0.0 85 12/1/2026
2.0.1 80 11/30/2026
2.0.0 79 11/30/2026
1.1.1 85 11/30/2026
1.1.0 84 11/30/2026
1.0.1 84 11/30/2026
1.0.0 87 11/30/2026