/// <summary>
 ///     Clone the specified ApplicationContext and create a new DependentApplicationContext />.
 /// </summary>
 /// <param name="context">The DependentApplicationContext to be cloned.</param>
 /// <returns>The DependentApplicationContext</returns>
 /// .
 public static DependentApplicationContext DepedentClone(this ApplicationContext context)
 {
     Guard.ArgumentNotNull(context, "context");
     var currentContext = context.ContextLocator.GetCurrentContext();
     var contextItemCollection = new ContextItemCollection();
     foreach (var current in currentContext)
     {
         var obj = current.Value;
         var cloneable = obj as ICloneable;
         if (cloneable != null)
         {
             obj = cloneable.Clone();
         }
         var contextItem = new ContextItem(current.Key, obj, current.IsLocal);
         if (current.ReadOnly)
         {
             contextItem.ReadOnly = true;
         }
         contextItemCollection.Add(contextItem);
     }
     return new DependentApplicationContext(contextItemCollection);
 }
 private void EnsureCanWrite(ContextItem contextItem)
 {
     Guard.ArgumentNotNull(contextItem, "contextItem");
     var contextItem2 = GetContextItem(contextItem.Key);
     if (contextItem2 != null && contextItem2.ReadOnly)
     {
         throw new InvalidOperationException(
             ResourceUtility.Format(Resources.ExceptionCannotChangeReadonlyContextItem));
     }
 }
 /// <summary>
 ///     Set context item inernally.
 /// </summary>
 /// <param name="contextItem">The context item.</param>
 protected abstract void SetContextItemCore(ContextItem contextItem);
 /// <summary>
 ///     Add a new context item or use the new context item to override the exiting one.
 /// </summary>
 /// <param name="contextItem">The new <see cref="T:Cedar.Core.ApplicationContexts.ContextItem" /> to set.</param>
 public void SetContextItem(ContextItem contextItem)
 {
     EnsureCanWrite(contextItem);
     SetContextItemCore(contextItem);
 }
 /// <summary>
 ///     Add a new context item or use the new context item to override the exiting one.
 /// </summary>
 /// <param name="contextItem">The new <see cref="T:Cedar.Core.ApplicationContexts.ContextItem" /> to set.</param>
 protected override void SetContextItemCore(ContextItem contextItem)
 {
     Guard.ArgumentNotNull(contextItem, "contextItem");
     if (SessionStateAvailabe)
     {
         HttpContext.Current.Session[contextItem.Key] = contextItem;
         if (!ContextItemKeys.Contains(contextItem.Key))
         {
             ContextItemKeys.Add(contextItem.Key);
         }
     }
     else
     {
         CallContextLocator.SetContextItem(contextItem);
     }
 }
 /// <summary>
 ///     Add a new context item or use the new context item to override the exiting one.
 /// </summary>
 /// <param name="contextItem">The new <see cref="T:Cedar.Core.ApplicationContexts.ContextItem" /> to set.</param>
 protected override void SetContextItemCore(ContextItem contextItem)
 {
     Guard.ArgumentNotNull(contextItem, "contextItem");
     CallContext.FreeNamedDataSlot(contextItem.Key);
     CallContext.SetData(contextItem.Key, contextItem);
     if (!Keys.Contains(contextItem.Key))
     {
         Keys.Add(contextItem.Key);
     }
 }