Multiple persistence throws exception

Hi,

After upgrading from v5 to v6 of NSB, one of mye endpoints now throws exception when using mulitple persistence.

In v5 this configuration did not throw exception:

configuration.UsePersistence<RavenDBPersistence>).SetDefaultDocumentStore(documentStore).For(Storage.Subscriptions);
configuration.UsePersistence<InMemoryPersistence>().For(Storage.Sagas, Storage.Timeouts, Storage.Outbox);

while in v6 this configuration throws exception:

endpointConfiguration.UsePersistence<RavenDBPersistence, StorageType.Subscriptions>().SetDefaultDocumentStore(documentStore);
endpointConfiguration.UsePersistence<InMemoryPersistence, StorageType.Sagas>();
endpointConfiguration.UsePersistence<InMemoryPersistence, StorageType.Timeouts>();
endpointConfiguration.UsePersistence<InMemoryPersistence, StorageType.Outbox>();

This is the exception:

Unable to cast object of type 'NServiceBus.Persistence.RavenDB.RavenDBSynchronizedStorageSession' to type 'NServiceBus.InMemorySynchronizedStorageSession.

So is this related to this: outbox

Hi Ivan,

I believe this is a bug. I have raised an issue for it here InvalidCastException thrown when mixing persistence · Issue #365 · Particular/NServiceBus.RavenDB · GitHub

It looks like the RavenDB Persistence adds behaviors into the pipeline to provide a synchronized storage session, even if they aren’t needed.

One thing I did notice, I was only able to replicate this issue when I was handling a message in a saga. Do you have sagas running in this endpoint?

The original problem could manifest with any other persistence but I was surprised to see In-Memory persistence in use with real sagas.

Regards,
Mike Minutillo
Particular Software

Hello,

Yepp, I do have sagas running in the endpoint handling one or more messages. And yes, the issue seems to occur when two different (note that I have only tested with RavenDb and InMemory) persistence are set for subscription and saga.

I have inherited the codebase, so I’m not sure why it was done this way, but my initial thought was that subscriptions, sagas and timeouts should kept within the same persistence, but it got me wondering since it worked in v5.