Getting Started
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Previews
Samples

Transactional Session with Azure Table Persistence

NuGet Package: NServiceBus.Persistence.AzureTable.TransactionalSession (3.x)
Target Version: NServiceBus 7.x

In order to use the TransactionalSession feature with Azure Table Persistence, add a reference to the NServiceBus.Persistence.AzureTable.TransactionalSession NuGet package.

Configuration

To enable the transactional session:

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

Opening a session

To open a Azure Storage Persistence transactional session:

using var childBuilder = builder.CreateChildBuilder();
var session = childBuilder.Build<ITransactionalSession>();
await session.Open(
        new AzureTableOpenSessionOptions(
            new TableEntityPartitionKey("MyPartitionKey")))
    .ConfigureAwait(false);

// use the session

await session.Commit()
    .ConfigureAwait(false);

Configuring the table

The name of the destination table can be specified when opening the session:

using var childBuilder = builder.CreateChildBuilder();
var session = childBuilder.Build<ITransactionalSession>();
await session.Open(
        new AzureTableOpenSessionOptions(
            new TableEntityPartitionKey("MyPartitionKey"),
            new TableInformation("MyTable")))
    .ConfigureAwait(false);

// use the session

await session.Commit()
    .ConfigureAwait(false);

Transaction usage

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

await session.Open(
        new AzureTableOpenSessionOptions(
            new TableEntityPartitionKey("MyPartitionKey")))
    .ConfigureAwait(false);

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

// access the database:
var azureTableSession = session.SynchronizedStorageSession.AzureTablePersistenceSession();

await session.Commit()
    .ConfigureAwait(false);

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

In order to guarantee atomic consistency across message and database operations, the Outbox needs to be enabled. Otherwise Commit executes database modifications first and then messages are dispatched with best-effort.

Related Articles


Last modified