Solutions    |    Downloads    |    License    |    Documentation    |    Training    |    Support    |    Customers    |    About Us

Unit Of Work In NServiceBus

Last Updated: Nov 27, 2012 05:41PM IST

When using a framework like NServiceBus there is usually a need to create your own unit of work in order to avoid having to repeat code over an over again in your message handlers.
Examples would be committing NHibernate transactions, calling SaveChanges on the RavenDB session, etc etc.

In NServiceBus 2.6 your only hook into the message pipeline was the message modules, you can read about them in Jonathan Oliver's post.
This had some quirks since the HandleEndMessage method of the message module will be invoked regardless of the outcome and made it hard to decide if a commit or rollback should be performed.

Starting from NServiceBus version 3.0 there is a new way to do this, meet the IManageUnitsOfWork interface

public interface IManageUnitsOfWork
{
    /// <summary>
    /// Called before all message handlers and modules
    /// </summary>
    void Begin();

    /// <summary>
    /// Called after all message handlers and modules
    /// </summary>
    void End(Exception ex = null);
}

The semantics are that that Begin() will be called when the transport messages enters the pipeline, remember that a transport message can consist of multiple application messages. This allows you to do any setup that is required.
The End() method is called when the processing is complete and if there was an exception that exception will be passed into the method.
This gives you a way to perform different actions depending on the outcome of the message(s).

About NServiceBus    |    Contact Us    |    Privacy    |    Follow us on:   
Copyright 2010-2013 NServiceBus. All rights reserved
NSB_Y@yahoo.com
http://assets3.desk.com/r1046ffeaa2233e531563a32d7edef6677d8a78b5/javascripts/
nservicebus
Loading
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
about
true
Invalid characters found
/customer/portal/articles/autocomplete
There was an error contacting Get Satisfaction
View All
0
discussions
replies
Questions
Ideas
Problems
Praise