Skip to content

Instantly share code, notes, and snippets.

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 masterpoi/4655544 to your computer and use it in GitHub Desktop.
Save masterpoi/4655544 to your computer and use it in GitHub Desktop.
Hybrid lifestyle for Rhino Service Bus
public class HybridPerWebRequestRhinoServiceBusScopeAccessor : HybridPerWebRequestScopeAccessor
{
public HybridPerWebRequestRhinoServiceBusScopeAccessor() :
base(new RhinoServiceBusSessionScopeAccessor()) { }
}
public class RhinoServiceBusSessionScopeAccessor : IScopeAccessor
{
#region IScopeAccessor Members
public void Dispose()
{
RhinoServiceBusLifestyleModule.EndScope();
}
public ILifetimeScope GetScope(CreationContext context)
{
return RhinoServiceBusLifestyleModule.Scope;
}
#endregion
}
public class RhinoServiceBusLifestyleModule : IMessageModule
{
[ThreadStatic]
private static ILifetimeScope _currentScope;
public static ILifetimeScope Scope
{
get
{
return _currentScope;
}
}
#region IMessageModule Members
public void Init(ITransport transport, IServiceBus bus)
{
transport.MessageArrived += TransportOnMessageArrived;
transport.MessageProcessingCompleted += TransportOnMessageProcessingCompleted;
}
public void Stop(ITransport transport, IServiceBus bus)
{
transport.MessageArrived -= TransportOnMessageArrived;
transport.MessageProcessingCompleted -= TransportOnMessageProcessingCompleted;
}
#endregion
internal static void EndScope()
{
if (_currentScope != null)
{
_currentScope.Dispose();
_currentScope = null;
}
}
private static void TransportOnMessageProcessingCompleted(CurrentMessageInformation currentMessageInformation, Exception exception)
{
EndScope();
}
private bool TransportOnMessageArrived(CurrentMessageInformation currentMessageInformation)
{
if (_currentScope != null)
throw new Exception("Bug in rhinoservicebuslifestylemodule. Scope is allready initialized. Should not happen");
_currentScope = new DefaultLifetimeScope();
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment