Configuration order for persistence

Component: NServiceBus
NuGet Package NServiceBus (5.x)
Standard support for version 5.x of NServiceBus has expired. For more information see our Support Policy.

When using different persistence options for storage types, the configuration order is important. When specifying multiple persistence options for the same storage type, the last-configured option will be used. Using the generic UsePersistence<TPersistenceOption> (without specifying a storage type) applies the persistence to all its supported storage types.

Example 1

In this example, RavenDB persistence is used for all storage types as it overwrites the configuration for the Outbox and GatewayDeduplication storage types.

5.x NServiceBus
busConfiguration.UsePersistence<NHibernatePersistence>()
    .For(Storage.Outbox);

busConfiguration.UsePersistence<InMemoryPersistence>()
    .For(Storage.GatewayDeduplication);

// This one will override the above settings
busConfiguration.UsePersistence<RavenDBPersistence>();
5.2 NServiceBus
busConfiguration.UsePersistence<NHibernatePersistence, StorageType.Outbox>();

busConfiguration.UsePersistence<InMemoryPersistence, StorageType.GatewayDeduplication>();

// This one will override the above settings
busConfiguration.UsePersistence<RavenDBPersistence>();

Example 2

To avoid overwriting, all storage types can be explicitly configured using the UsePersistence<TPersistenceOption, TStorageType> API.

5.x NServiceBus
busConfiguration.UsePersistence<NHibernatePersistence>()
    .For(Storage.Outbox);

busConfiguration.UsePersistence<InMemoryPersistence>()
    .For(Storage.GatewayDeduplication);

busConfiguration.UsePersistence<RavenDBPersistence>()
    .For(Storage.Sagas,
        Storage.Subscriptions,
        Storage.Timeouts);
5.2 NServiceBus
busConfiguration.UsePersistence<NHibernatePersistence, StorageType.Outbox>();

busConfiguration.UsePersistence<InMemoryPersistence, StorageType.GatewayDeduplication>();

busConfiguration.UsePersistence<RavenDBPersistence, StorageType.Sagas>();
busConfiguration.UsePersistence<RavenDBPersistence, StorageType.Subscriptions>();
busConfiguration.UsePersistence<RavenDBPersistence, StorageType.Timeouts>();

Example 3

Instead of explicitly defining all storage types, the generic persistence option can specified before the explicit overwrites. In this example, RavenDB persistence will be used for all storage types except for the Outbox and GatewayDeduplication.

5.x NServiceBus
busConfiguration.UsePersistence<RavenDBPersistence>();

busConfiguration.UsePersistence<NHibernatePersistence>()
    .For(Storage.Outbox);

busConfiguration.UsePersistence<InMemoryPersistence>()
    .For(Storage.GatewayDeduplication);
5.2 NServiceBus
busConfiguration.UsePersistence<RavenDBPersistence>();

busConfiguration.UsePersistence<NHibernatePersistence, StorageType.Outbox>();

busConfiguration.UsePersistence<InMemoryPersistence, StorageType.GatewayDeduplication>();

Last modified