CallContext

详情:http://www.cnblogs.com/waw/archive/2011/08/29/2158763.html

例一:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LearnCallContext
{
    class Program
    {
        static void Main(string[] args)
        {
            // CallContext:实现对上下文信息的存储
            // 在主线程存储的数据,可以在主线程的生命周期内任何地方访问
            CallContext.SetData("Name", "shz"); 
            CallContext.SetData("Age", 17);

            // 在主线程存储的数据,可以在主线程和子线程的生命周期内任何地方访问
            CallContext.LogicalSetData("LogicalName", "tom");
            CallContext.LogicalSetData("LogicalAge", 28);

            GetDataInMainThread();

            Thread t1 = new Thread(GetDataInSonThread);
            t1.Start();

            Console.ReadKey();
        }

        static void GetDataInMainThread()
        {
            object name = CallContext.GetData("Name");
            object age = CallContext.GetData("Age");
            object logicalName = CallContext.LogicalGetData("LogicalName");
            object logicalAge = CallContext.LogicalGetData("LogicalAge");

            Console.WriteLine("============ UI Thread===================");
            Console.WriteLine("Name:" + name);
            Console.WriteLine("Age:" + age);
            Console.WriteLine("LogicalName:" + logicalName);
            Console.WriteLine("LogicalAge:" + logicalAge);
            Console.WriteLine();
        }

        static void GetDataInSonThread()
        {
            object name = CallContext.GetData("Name");
            object age = CallContext.GetData("Age");
            object logicalName = CallContext.LogicalGetData("LogicalName");
            object logicalAge = CallContext.LogicalGetData("LogicalAge");

            Console.WriteLine("============ Son Thread===================");
            Console.WriteLine("Name:" + name);
            Console.WriteLine("Age:" + age);
            Console.WriteLine("LogicalName:" + logicalName);
            Console.WriteLine("LogicalAge:" + logicalAge);
            Console.WriteLine();            
        }
    }
}
View Code

运行结果:

例二:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HttpContextTest
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // 默认只能在主线程中访问Session对象,子线程不可以
            HttpContext.Current.Session["Name"] = "shz";
            // 若要使子线程也能访问Session对象,可以通过CallContext.LogicalSetData方法存储当前上下文对象,
            // 这样子线程也就可以访问到Session对象了
            CallContext.LogicalSetData("HttpContext", HttpContext.Current);

            //Thread t1 = new Thread(AccessHttpContextInSonThread);
            //t1.Start();

            Thread t2 = new Thread(AccessHttpContextInSonThreadByCallContext);
            t2.Start();
        }

        private void AccessHttpContextInSonThread()
        {
            if(HttpContext.Current == null)
            {
                this.lblMsg.Text = "HttpContext.Current 为空";   
            }
            else
            {
                this.lblMsg.Text = HttpContext.Current.Session["Name"].ToString();
            }
        }

        private void AccessHttpContextInSonThreadByCallContext()
        {
            HttpContext context = CallContext.LogicalGetData("HttpContext") as HttpContext;

            if (context == null)
            {
                this.lblMsg.Text = "CallContext中存储的HttpContext.Current 为空";
            }
            else
            {
                this.lblMsg.Text = context.Session["Name"].ToString();
            }
        }
    }
}
View Code
原文地址:https://www.cnblogs.com/shaomenghao/p/4160488.html