Created
January 28, 2013 13:36
Hybrid lifestyle for Rhino Service Bus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HybridPerWebRequestRhinoServiceBusScopeAccessor : HybridPerWebRequestScopeAccessor | |
{ | |
public HybridPerWebRequestRhinoServiceBusScopeAccessor() : | |
base(new RhinoServiceBusSessionScopeAccessor()) { } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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