SynchronizedStorageSession and IoC

Hi,

We are currently migrating from MSMQ and DTC to RabbitMQ with an MSSQL outbox. As I understand it, the only way to have the outbox and business logic share the same transaction is to get the SQL connection and SQL transaction from the SynchronizedStorageSession and use it for business DB access. Our repository is currently registered in the IoC container, arriving to the handler as a constructor parameter. The SynchronizedStorageSession comes as a property on IMessageHandlerContext, that is, as a handler method parameter. Is there any way to get the SynchronizedStorageSession (or just the connection) into the container so that we can keep using constructor dependency injection?

An alternative (and probably better solution) would be to have the outbox connection enlisted in the TransactionScope. Is there any way to achieve this? I know about the UnitOfWork feature, but as far as I understand, the outbox will not be part of its transaction.

Thanks,
/Torbjörn

Unfortunately there is no OOTB way on dealing with this but there is a solution that does work with DI

The pipeline is extended with a StorageContextBehavior behavior. It resolves the storage connection and transaction objects as would be needed in a regular V6 handler via the just before the handlers are invoked and assigns the values to the by the container created StorageContext object. The StorageContext object is registered as DependencyLifecycle.InstancePerUnitOfWork and the repository OrderRepository repository is dependant on it. When the handler object graph is created by the container, all potential repository objects will share the same StorageContext object instance. This way all created repository objects or any other object that would take a dependeny on StorageContext can now access the shared IDbTransaction and IDbConnection to make sure all queries participate on the same connection and unit of work.

Let me know if you have any question.

Thanks for the answer.

As a follow up question, what is the recommended way to implement message handlers using RabbitMQ? Is it to use the SynchronizedStorageSession and let business operations hook into the outbox transaction? Or is it to write idempotent handlers (which sort of makes the outbox redundant)?

@toroso both the outbox and idempotent handlers are great.

However, when not using the outbox make sure that an sends/publishes will be sending the exact same messages. Depending on the type of message you might also need to set the message identity as documented at the following location:

https://docs.particular.net/nservicebus/messaging/message-identity

– Ramon