Example #1
0
        /// <summary>
        /// Attaches this instance.
        /// </summary>
        protected void Attach()
        {
            if (HttpContext.Current != null)
            {
                // Use HttpContext to attach this object to since when using ASP.NET, requests can be migrated
                // from the thread-pool to an internal queue and back and it will make us lose our context.
                //
                // see: http://piers7.blogspot.com/2005/11/threadstatic-callcontext-and_02.html
                var existingContext = (ContextBoundObject <T>)HttpContext.Current.Items[ContextKey];

                if (existingContext != null)
                {
                    this.Parent = existingContext;
                }

                HttpContext.Current.Items[ContextKey] = this;
            }
            else
            {
                var existingContext = (ContextBoundObject <T>)CallContext.LogicalGetData(ContextKey);

                if (existingContext != null)
                {
                    this.Parent = existingContext;
                }

                CallContext.LogicalSetData(ContextKey, this);
            }
        }
Example #2
0
        /// <summary>
        /// Detaches this instance.
        /// </summary>
        public void Detach()
        {
            if (HttpContext.Current != null)
            {
                // Use HttpContext to attach this object to since when using ASP.NET, requests can be migrated
                // from the thread-pool to an internal queue and back and it will make us lose our context.
                //
                // see: http://piers7.blogspot.com/2005/11/threadstatic-callcontext-and_02.html
                HttpContext.Current.Items[ContextKey] = this.Parent;
            }
            else
            {
                if (this.Parent != null)
                {
                    CallContext.LogicalSetData(ContextKey, this.Parent);
                }
                else
                {
                    CallContext.FreeNamedDataSlot(ContextKey);
                }
            }

            this.Parent = null;
        }