Unit of work using the pipeline

Introduction

This sample leverages the pipeline provide unit of work management for message handlers. Using the pipeline instead of the IManageUnitsOfWork abstraction is necessary when access to the incoming message and/or headers is required.

This sample simulates a multi-tenant solution where the session provide to handlers is connects to individual tenant databases based on the value of a tenant header on the incoming message.

Note the injected session factory is responsible for creating the session and that the session is registered the pipeline context using context.Extensions.Set<IMySession>(session);. This will be used later to provide the session to the handlers.

Registering the behavior

The following code is needed to register the behavior in the receive pipeline.

Providing the session to handlers

While it is possible to use the code contex.Extensions.Get<IMySession> in the handler, it is better to provide extension methods on IMessageHandlerContext to allow for a more terse syntax. In this sample the methods .Store<T> a .GetSession are provided:

Running the sample

Run the sample. Once running press any key to send messages. Note that for each given message the two message handlers will get the same session instance and that the instance is connected to the given tenant specified on the incoming message.