占位符在C#中的线程上下文存储温度值 [英] placeholder for storing temp values in c# thread context

查看:147
本文介绍了占位符在C#中的线程上下文存储温度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要存储在C#中的线程上下文或类似的(就像HttpContext的一个Web请求)一些信息(键值对)。

I want to store some info (key value pair) in a c# thread context or similar ( just like httpcontext for a web request ).

我想成为能够存储该信息(键值对)线程上下文(或类似的东西),使我的代码总是可以读取当前线程其下运行的这些值的某个地方。

I want to be able to store this info (key value pair) somewhere in thread context ( or something similar) so that my code can always read these values from the current thread its running under.

在我appliation我有API的调用链及其不可能从一个方法到其他通过该信息(已dsicarded这个选项!)

In my appliation i have a chain of API calls and its not possible to pass this info from one method to other ( already dsicarded this option! )

注意 - 这个线程运行作为一个Asp.Net应用程序中的异步操作。

note - this thread is running as an async operation inside an Asp.Net application.

推荐答案

您寻找< A HREF =http://msdn.microsoft.com/en-us/library/6sby1byh.aspx相对=nofollow>螺纹使用的 ThreadStatic属性的?

public static class ThreadLocalExample
{
    // There will be one Foo instance per thread.
    // Each thread will have to initialize it's own instance.
    [ThreadStatic]
    private static Foo bar;
}



当然,你可以添加一些辅助方法或属性上面来帮助你管理实例,包括确保在每个线程上的初始化。

Of course you could add some helper methods or properties to the above to help you manage the instance, including ensuring initialization on each thread.

这篇关于占位符在C#中的线程上下文存储温度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆