WCF请求处理线程敏捷吗? [英] Are WCF request handling Thread Agile?
问题描述
我已经看到了很多有关如何处理Asp.Net请求的文档?我想知道WCF请求处理的情况是否相同.我们可以依靠启动Wcf请求处理的线程会完成它的事实吗?
I have seen lots of documentation on how Agile Asp.Net Request handling is? I want to know is the case same with WCF Request handling. Can we rely on the fact that the Thread that starts Wcf request handling will finish it?
我正在维护一个Wcf应用程序,在很多地方都使用ThreadStatic变量.虽然代码可以正常工作,但它可靠吗?是值得进行更改还是应该保持原样?
I am maintaining a Wcf Application where at lots of places ThreadStatic variables are used. Although the code is working but is it reliable? Is it worth changing it or should I keep it as it is?
推荐答案
创建WCF服务时,可以通过使用ServiceBehavior
属性修饰服务实现类来设置线程和服务实例化行为:
When creating a WCF service you can set the threading and service instantiating behaviour, by decorating the service implementation class with a ServiceBehavior
attribute:
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single)]
class SingleCachingHttpFetcher : IHttpFetcher
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single)]
class SingleCachingHttpFetcher : IHttpFetcher
EDIT
I dit a bit more research and found this article: http://blogs.microsoft.co.il/blogs/applisec/archive/2009/11/23/wcf-thread-affinity-and-synchronization.aspx. It basically says that no, you cannot be sure that the same thread starting the request will be the one finishing it.
编辑2
之前已经在 StackOverflow 中讨论了此问题.它链接到如何制作WCF服务STA(单线程) ),其中描述了如何创建将强制使用单线程单元的OperationBehavior.该示例处理了调用GUI组件的问题,但它也应适用于其他单线程要求.
EDIT 2
This question has been discussed before at StackOverflow. It links to How to make a WCF service STA (single-threaded) where there is a description on how to create an OperationBehavior which will force a single threaded apartment. The example deals with calling GUI components, but it should work for other single threaded requirements as well.
这篇关于WCF请求处理线程敏捷吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!