public void SetAndGetContexts1() { string name = Guid.NewGuid().ToString(); string value1 = Guid.NewGuid().ToString(); string value2 = Guid.NewGuid().ToString(); //1. Outside of ExecutionContextScope: ExecutionContext.Current = null Assert.IsNull(ExecutionContext.Current); //2. Current ExecutionContext is avilable in the ExecutionContextScope. using (ExecutionContextScope contextScope = new ExecutionContextScope()) { ExecutionContext.Current.SetValue(name, value1); Assert.AreEqual <string>(value1, ExecutionContext.Current.GetValue <string>(name)); } //3. Nested ExecutionContextScope: ExecutionContextOption.Required using (ExecutionContextScope contextScope1 = new ExecutionContextScope()) { ExecutionContext.Current.SetValue(name, value1); using (ExecutionContextScope contextScope2 = new ExecutionContextScope(ExecutionContextOption.Required)) { Assert.AreEqual <string>(value1, ExecutionContext.Current.GetValue <string>(name)); ExecutionContext.Current.SetValue(name, value2); Assert.AreEqual <string>(value2, ExecutionContext.Current.GetValue <string>(name)); } Assert.AreEqual <string>(value2, ExecutionContext.Current.GetValue <string>(name)); } //4. Nested ExecutionContextScope: ExecutionContextOption.RequiresNew using (ExecutionContextScope contextScope1 = new ExecutionContextScope()) { ExecutionContext.Current.SetValue(name, value1); using (ExecutionContextScope contextScope2 = new ExecutionContextScope(ExecutionContextOption.RequiresNew)) { Assert.IsNotNull(ExecutionContext.Current); Assert.IsNull(ExecutionContext.Current.GetValue <string>(name)); ExecutionContext.Current.SetValue(name, value2); Assert.AreEqual <string>(value2, ExecutionContext.Current.GetValue <string>(name)); } Assert.AreEqual <string>(value1, ExecutionContext.Current.GetValue <string>(name)); } //5. Nested ExecutionContextScope: ExecutionContextOption.Supress using (ExecutionContextScope contextScope1 = new ExecutionContextScope()) { ExecutionContext.Current.SetValue(name, value1); using (ExecutionContextScope contextScope2 = new ExecutionContextScope(ExecutionContextOption.Suppress)) { Assert.IsNull(ExecutionContext.Current); } Assert.AreEqual <string>(value1, ExecutionContext.Current.GetValue <string>(name)); } }
void TestDocumentWithRules(string docPath, string rulePath) { ExtractTaskDocument taskDoc = new ExtractTaskDocument(rulePath).BindRules(); taskDoc.ExtractArguments.Add("SiteUrl", "https://www.amazon.co.uk"); taskDoc.ExtractArguments.Add("Asin", "B009A4A8ZO"); taskDoc.ExtractArguments.Add("SellerId", "APZXIW0LSZ4VN"); //Document rootDoc = extraTask.GetStartupDocument(); string html = File.ReadAllText(docPath); Document rootDoc = NSoupClient.Parse(html); fetchPageData: using (ExecutionContextScope scope = new ExecutionContextScope()) { ExtractDocumentReport report = taskDoc.ExtractWith(rootDoc); if (!report.IsSuccess()) { throw report.ExtractExcetpion; } else { ExtractPagerNode node = taskDoc.GetPagerNode(); if (node != null) { List <string> nextUrls = node.GetPageUrlList(); if (node.PageListType == PagerType.ByNext) { if (nextUrls.Any()) { taskDoc.DocumentUrl = nextUrls[0]; rootDoc = taskDoc.GetDocumentByUrl(taskDoc.DocumentUrl); goto fetchPageData; } } else { string currentDocUrl = taskDoc.EntryUrl.GetUrl(); } } } } }
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)); } }
internal ExecutionContextScope(ExecutionContextScopeManager manager, ExecutionContextScope parentScope) : base(manager.Container) { this.manager = manager; this.ParentScope = parentScope; }
public void Test() { using (ExecutionContextScope scope = new ExecutionContextScope()) { } }
internal ExecutionContextScope(ExecutionContextScopeManager manager, ExecutionContextScope parentScope) { this.manager = manager; this.ParentScope = parentScope; }