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; }
public ILifetimeScope GetScope(CreationContext context) { var selected = context.SelectScopeRoot(scopeRootSelector); if (selected == null) { throw new InvalidOperationException( string.Format( "Scope was not available for '{0}'. No component higher up in the resolution stack met the criteria specified for scoping the component. This usually indicates a bug in custom scope root selector or means that the component is being resolved in a unforseen context (a.k.a - it's probably a bug in how the dependencies in the application are wired).", componentModel.Name)); } var stash = (DefaultLifetimeScope)selected.GetContextualProperty(ScopeStash); if (stash == null) { DefaultLifetimeScope newStash = null; newStash = new DefaultLifetimeScope(new ScopeCache(), burden => { if (burden.RequiresDecommission) { selected.Burden.RequiresDecommission = true; selected.Burden.GraphReleased += delegate { newStash.Dispose(); }; } }); selected.SetContextualProperty(ScopeStash, newStash); stash = newStash; } return(stash); }