Inheritance: ILifetimeScope
コード例 #1
0
ファイル: RebusScopeAccessor.cs プロジェクト: nls75/Rebus
        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;
        }
コード例 #2
0
        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);
        }
コード例 #3
0
 private static ILifetimeScope GetOrCreateSessionScope(HttpSessionStateBase session) {
     var scope = (ILifetimeScope) session[lifetimeScopeKey];
     if (scope != null)
         return scope;
     scope = new DefaultLifetimeScope();
     session[lifetimeScopeKey] = scope;
     return scope;
 }
コード例 #4
0
ファイル: PerTransportMessage.cs プロジェクト: nls75/Rebus
        public ILifetimeScope GetScope(CreationContext context)
        {
            if (!MessageContext.HasCurrent)
                return null;

            return scopes.GetOrAdd(MessageContext.GetCurrent(), messageContext =>
            {
                var scope = new DefaultLifetimeScope(new ScopeCache());
                messageContext.Disposed += scope.Dispose;
                return scope;
            });
        }
コード例 #5
0
        private static ILifetimeScope GetScope(HttpSessionState session, bool createIfNotPresent)
        {
            var lifetimeScope = (ILifetimeScope)session[key];

            if (lifetimeScope == null && createIfNotPresent)
            {
                lifetimeScope = new DefaultLifetimeScope(new ScopeCache(), null);
                session[key] = lifetimeScope;
                return lifetimeScope;
            }

            return lifetimeScope;
        }
コード例 #6
0
        public ILifetimeScope GetScope(HttpContextBase httpContext)
        {
            var cookie = httpContext.Request.Cookies["LifeStyle"];
            ILifetimeScope currentScope;
            if (cookie == null)
            {
                currentScope = new DefaultLifetimeScope(new ScopeCache());
                string key = Guid.NewGuid().ToString();
                _knownReferences.SetItem(key, currentScope);
                httpContext.Request.Cookies.Add(new HttpCookie("LifeStyle", key));
            }
            else
                currentScope = _knownReferences.GetItem(cookie.Value);

            return currentScope;
        }
コード例 #7
0
        public ILifetimeScope GetScope(CreationContext context)
        {
            foreach (var additionalArgument in context.AdditionalArguments.Values)
            {
                if (additionalArgument is ServiceStub)
                {
                    var serviceStub = additionalArgument as ServiceStub;

                    if (_createdScopes.ContainsKey(serviceStub))
                        return _createdScopes[serviceStub];
                    else
                    {
                        var scope = new DefaultLifetimeScope();
                        _createdScopes[serviceStub] = scope;
                        serviceStub.Disposing += RemoveScope;

                        return scope;
                    }
                }
            }

            throw new InvalidOperationException("This was not used as intented, I am expecting a servicestub as a provided argument");
        }
 public void ClearScope() {
     var newScope = new DefaultLifetimeScope();
     var oldScope = Interlocked.Exchange(ref scope, newScope);
     oldScope.Dispose();
 }
コード例 #9
0
		private static ILifetimeScope GetScope(HttpContext context, bool createIfNotPresent)
		{
			var candidates = (ILifetimeScope)context.Items[key];
			if (candidates == null && createIfNotPresent)
			{
				candidates = new DefaultLifetimeScope(new ScopeCache());
				context.Items[key] = candidates;
			}
			return candidates;
		}
コード例 #10
0
		public static void ResetScope()
		{
			scope = new DefaultLifetimeScope();
		}