Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Accessing data via NHibernate persistence

Target Version: NServiceBus 7.x

NHibernate persistence supports a mechanism that allows using the same data context used by NServiceBus internals to also store business data. This ensures atomicity of changes done across multiple handlers and sagas involved in processing of the same message. See accessing data to learn more about other ways of accessing the data in the handlers.

NServiceBus-managed ISession instance can be accessed using the handler context through context.SynchronizedStorageSession.Session() or by using the dependency injection (DI).

Using in a handler

public class OrderHandler :
    IHandleMessages<OrderMessage>
{
    public Task Handle(OrderMessage message, IMessageHandlerContext context)
    {
        var nhibernateSession = context.SynchronizedStorageSession.Session();
        nhibernateSession.Save(new Order());
        return Task.CompletedTask;
    }
}
public class OrderHandler :
    IHandleMessages<OrderMessage>
{
    INHibernateStorageSession synchronizedStorageSession;

    public OrderHandler(INHibernateStorageSession synchronizedStorageSession)
    {
        this.synchronizedStorageSession = synchronizedStorageSession;
    }

    public Task Handle(OrderMessage message, IMessageHandlerContext context)
    {
        synchronizedStorageSession.Session.Save(new Order());
        return Task.CompletedTask;
    }
}
A helper type INHibernateStorageSession is used in the example above because by default ISession is not registered in the DI container. Users who wish to register ISession need to add the following line to the endpoint configuration:
config.RegisterComponents(c =>
{
    c.ConfigureComponent(b =>
    {
        var session = b.Build<INHibernateStorageSession>();
        var repository = new MyRepository(session.Session);
        return repository;
    }, DependencyLifecycle.InstancePerUnitOfWork);
});

Using in a saga

Other than interacting with its own internal state, a saga should not access a database, call out to web services, or access other resources. See Accessing databases and other resources from a saga.

If the situation is special enough to warrant going against this recommendation, the following documentation will describe how to do so.

public class OrderSaga :
    Saga<OrderSaga.SagaData>,
    IHandleMessages<OrderMessage>
{
    public Task Handle(OrderMessage message, IMessageHandlerContext context)
    {
        var nhibernateSession = context.SynchronizedStorageSession.Session();
        nhibernateSession.Save(new Order());
        return Task.CompletedTask;
    }

Regardless of how the ISession object is accessed, it is fully managed by NServiceBus according to the best practices defined by NServiceBus documentation with regards to transactions.

Related Articles