Getting Started
Architecture
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Transactional Session with MongoDB Persistence

NuGet Package: NServiceBus.Storage.MongoDB.TransactionalSession (3.x)
Target Version: NServiceBus 8.x

In order to use the transactional session feature with MongoDB persistence, add a reference to the NServiceBus.Storage.MongoDB.TransactionalSession NuGet package.

Configuration

To enable the transactional session feature:

var persistence = config.UsePersistence<MongoPersistence>();
persistence.EnableTransactionalSession();

Opening a session

To open a MongoDB transactional session:

using var childScope = serviceProvider.CreateScope();
var session = childScope.ServiceProvider.GetService<ITransactionalSession>();
await session.Open();

// use the session

await session.Commit();

Transaction usage

Message and database operations made via the the transactional session are committed together once the session is committed:

await session.Open();

// add messages to the transaction:
await session.Send(new MyMessage());

// access the database:
var mongoSession = session.SynchronizedStorageSession.MongoPersistenceSession();

await session.Commit();

See the MongoDB persistence transactions documentation for further details about using the MongoDB transaction.

Related Articles