public ILifetimeScope GetScope(CreationContext context)
{
var messageContext = MessageContext.Current;
if (messageContext == null)
{
throw new InvalidOperationException(string.Format("Attempted to resolve {0} outside of Rebus message context!", context.RequestedType));
}
var items = messageContext.TransactionContext.Items;
object lifetimeScope;
if (items.TryGetValue(LifestimeScopeItemKey, out lifetimeScope))
{
return (ILifetimeScope) lifetimeScope;
}
var defaultLifetimeScope = new DefaultLifetimeScope();
items[LifestimeScopeItemKey] = defaultLifetimeScope;
messageContext.TransactionContext.OnDisposed(() => defaultLifetimeScope.Dispose());
return defaultLifetimeScope;
}