Skip to content

Instantly share code, notes, and snippets.

@jbogard
Created December 7, 2011 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbogard/1443003 to your computer and use it in GitHub Desktop.
Save jbogard/1443003 to your computer and use it in GitHub Desktop.
public class RavenDbMessageModule : IMessageModule
{
private readonly IDocumentStore documentStore;
[ThreadStatic]
private static IDocumentSession currentSession;
public static IDocumentSession CurrentSession
{
get { return currentSession; }
}
public RavenDbMessageModule(IDocumentStore documentStore)
{
this.documentStore = documentStore;
}
public void Init(ITransport transport, IServiceBus serviceBus)
{
transport.MessageArrived += TransportOnMessageArrived;
transport.MessageProcessingCompleted += TransportOnMessageProcessingCompleted;
}
public void Stop(ITransport transport, IServiceBus serviceBus)
{
transport.MessageArrived -= TransportOnMessageArrived;
transport.MessageProcessingCompleted -= TransportOnMessageProcessingCompleted;
}
private static void TransportOnMessageProcessingCompleted(CurrentMessageInformation currentMessageInformation, Exception exception)
{
if (currentSession != null)
{
if (exception == null)
currentSession.SaveChanges();
currentSession.Dispose();
}
currentSession = null;
}
private bool TransportOnMessageArrived(CurrentMessageInformation currentMessageInformation)
{
if (currentSession == null)
currentSession = documentStore.OpenSession();
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment