我可以(安全地)使用 ADO.NET 数据服务中的 ThreadStatic 属性吗?
我想将每线程数据存储在 ADO.NET 数据服务中。在线程特定的静态变量上使用 ThreadStatic 属性是否安全,或者我会遇到问题吗?我担心的是,请求完成且线程终止后,我的 ThreadStatic 变量不会被垃圾收集。
如果有更好的方法来完成我想做的事情,请告诉我。这似乎是最简单的解决方案。
任何信息都会非常有帮助,谢谢!
I want to store per-thread data in an ADO.NET Data Service. Is it safe to use the ThreadStatic attribute on my thread-specific static variable, or will I run into problems? My concern is that my ThreadStatic variable(s) won't be garbage collected after the request is completed and the thread dies.
If there's a better way to do what I'm trying to do, please let me know. This just seems like the simplest solution.
Any information would be very helpful, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现为每个请求创建的 DataService 对象位于标记有
IsThreadPoolThread = true
的线程上,因此在这种情况下使用[ThreadStatic]
属性是不合适的,因为来自先前请求的信息可以被后来的请求获得(不可取)。I've found that the DataService object that's created for each request is on a thread marked with
IsThreadPoolThread = true
, so using the[ThreadStatic]
attribute is not appropriate in this case, as information from previous requests could be available by later ones (not desirable).任何标记有 ThreadStatic 的变量的生命周期至少与创建它们的线程一样长。线程终止后,它们会像任何其他值一样受到垃圾收集。
Any variables which are tagged with
ThreadStatic
will have the lifetime at least as long as the thread on which they are created. After that Thread terminates they are subject to garbage collection like any other value.