Exemple #1
0
        public void SetAndGetContexts2()
        {
            string name   = Guid.NewGuid().ToString();
            string value1 = Guid.NewGuid().ToString();
            string value2 = Guid.NewGuid().ToString();

            //1. Change current ExecutionContext will never affect the DependentContext.
            using (ExecutionContextScope contextScope1 = new ExecutionContextScope())
            {
                ExecutionContext.Current.SetValue(name, value1);
                DependentContext depedencyContext = ExecutionContext.Current.DepedentClone();
                ExecutionContext.Current.SetValue(name, value2);

                Task <string> task = Task.Factory.StartNew <string>(() =>
                {
                    using (ExecutionContextScope contextScope2 = new ExecutionContextScope(depedencyContext))
                    {
                        return(ExecutionContext.Current.GetValue <string>(name));
                    }
                });

                Assert.AreEqual <string>(value1, task.Result);
                Assert.AreEqual <string>(value2, ExecutionContext.Current.GetValue <string>(name));
            }

            //2. Change DependentContext will never affect the current ExecutionContext.
            using (ExecutionContextScope contextScope1 = new ExecutionContextScope())
            {
                ExecutionContext.Current.SetValue(name, value1);
                DependentContext depedencyContext = ExecutionContext.Current.DepedentClone();
                Task <string>    task             = Task.Factory.StartNew <string>(() =>
                {
                    using (ExecutionContextScope contextScope2 = new ExecutionContextScope(depedencyContext))
                    {
                        ExecutionContext.Current.SetValue(name, value2);
                        return(ExecutionContext.Current.GetValue <string>(name));
                    }
                });

                Assert.AreEqual <string>(value2, task.Result);
                Assert.AreEqual <string>(value1, ExecutionContext.Current.GetValue <string>(name));
            }
        }
 public AddNewDependentHandler(DependentContext db, IMapper mapper)
 {
     _db     = db;
     _mapper = mapper;
 }