コード例 #1
0
        /// <summary>
        /// Stores the given value into backing store for retrieval later.
        /// </summary>
        /// <param name="newValue">The object being stored.</param>
        public override void SetValue(object newValue)
        {
            if (OperationContext.Current != null)
            {
                //WCF without HttpContext environment
                ContainerExtension containerExtension = OperationContext.Current.Extensions.Find <ContainerExtension>();
                if (containerExtension == null)
                {
                    containerExtension = new ContainerExtension()
                    {
                        Value = newValue
                    };

                    OperationContext.Current.Extensions.Add(containerExtension);
                }
            }
            else if (HttpContext.Current != null)
            {
                //HttpContext avaiable ( ASP.NET ..)
                if (HttpContext.Current.Items[key.ToString()] == null)
                {
                    HttpContext.Current.Items[key.ToString()] = newValue;
                }
            }
            else
            {
                //Not in WCF or ASP.NET Environment, UnitTesting, WinForms, WPF etc.
                CallContext.SetData(key.ToString(), newValue);
            }
        }
コード例 #2
0
        /// <summary>
        /// Retrieve a value from the backing store associated with this Lifetime policy.
        /// </summary>
        /// <returns>The object desired, or null if no such object is currently stored.</returns>
        public override object GetValue()
        {
            object result = null;

            //Get object depending on  execution environment ( WCF without HttpContext,HttpContext or CallContext)

            if (OperationContext.Current != null)
            {
                //WCF without HttpContext environment
                ContainerExtension containerExtension = OperationContext.Current.Extensions.Find <ContainerExtension>();
                if (containerExtension != null)
                {
                    result = containerExtension.Value;
                }
            }
            else if (HttpContext.Current != null)
            {
                //HttpContext avaiable ( ASP.NET ..)
                if (HttpContext.Current.Items[key.ToString()] != null)
                {
                    result = HttpContext.Current.Items[key.ToString()];
                }
            }
            else
            {
                //Not in WCF or ASP.NET Environment, UnitTesting, WinForms, WPF etc.
                result = CallContext.GetData(key.ToString());
            }

            return(result);
        }
コード例 #3
0
 /// <summary>
 /// Remove the given object from backing store.
 /// </summary>
 public override void RemoveValue()
 {
     if (OperationContext.Current != null)
     {
         //WCF without HttpContext environment
         ContainerExtension containerExtension = OperationContext.Current.Extensions.Find <ContainerExtension>();
         if (containerExtension != null)
         {
             OperationContext.Current.Extensions.Remove(containerExtension);
         }
     }
     else if (HttpContext.Current != null)
     {
         //HttpContext avaiable ( ASP.NET ..)
         if (HttpContext.Current.Items[key.ToString()] != null)
         {
             HttpContext.Current.Items[key.ToString()] = null;
         }
     }
     else
     {
         //Not in WCF or ASP.NET Environment, UnitTesting, WinForms, WPF etc.
         CallContext.FreeNamedDataSlot(key.ToString());
     }
 }