Getting Started

Unit of Work

Component: NServiceBus
NuGet Package: NServiceBus (8.1)

Using a transaction scope

If a business transaction is spread across multiple handlers there is always a risk of partial updates since one handler might succeed in updating the data while other won't. To avoid this it is possible to use a unit of work that wraps all handlers in a TransactionScope and makes sure that there are no partial updates. Use following code to enable a wrapping scope:

var unitOfWork = endpointConfiguration.UnitOfWork();
This requires the selected persistence to support enlisting in transaction scopes.
This might escalate to a distributed transaction if data in different databases are updated.
This API must not be used in combination with transports running in transaction scope mode. Wrapping handlers in a TransactionScope in such a situation throws an exception.

Controlling transaction scope options

The following options for transaction scopes used to wrap all handlers can be configured.

Isolation level

NServiceBus will by default use the ReadCommitted isolation level.

Change the isolation level using

var unitOfWork = endpointConfiguration.UnitOfWork();
    isolationLevel: IsolationLevel.RepeatableRead);

Transaction timeout

NServiceBus will use the default transaction timeout of the machine the endpoint is running on.

Change the transaction timeout using

var unitOfWork = endpointConfiguration.UnitOfWork();
    timeout: TimeSpan.FromSeconds(30));

Or via .config file using a example DefaultSettingsSection.

Implementing custom unit of work

In scenarios, when custom unit of work is needed (e.g. to commit NHibernate transactions, or call SaveChanges on a RavenDB session without polluting handers logic) it can be implemented using a dedicated pipeline behavior.


Last modified