CallContext和ThreadStatic有什么区别?
我知道在ASP.NET环境中,存储在CallContext中的数据可以在整个请求中持久存在,直到它结束,而ThreadStatic可能会或可能不会工作,因为请求可能会切换线程.我还了解到HttpContext是使用CallContext在内部存储的.
在常规应用程序中,它们似乎都在同一个线程调用中持久存在.什么时候不是这样的?
编辑:在评论中我了解到调用上下文是线程静态存储的抽象.ASP.NET框架显式地将数据从一个线程移动到下一个处理一个请求的线程.其他想要提供线程敏捷性的框架可以对上下文存储执行相同的操作.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Taking data from Main Thread\n->");
string message = Console.ReadLine();
ThreadStart newThread = new ThreadStart(delegate { Write(message); });
Thread myThread = new Thread(newThread);
}
public static void Write(string msg)
{
Console.WriteLine(msg);
Console.Read();
}
}
}
Run Code Online (Sandbox Code Playgroud) 这真的是对这个问题的跟进.其中一个Simple Injector开发人员指出了一个关于Simple Injector的有用信息,我认为让它更易于访问是件好事.
那么,使用Web API 1和.NET 4.0支持Simple Injector是否有任何技术障碍?源代码很容易下载和编译.它似乎工作得很好.