5

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?

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?

Amitabh
  • 59,111
  • 42
  • 110
  • 159
  • 2
    Why is this relevant to you? What problem are you trying to solve by knowing this? You basically send a message to a WCF service and get back a response - why is it important to you to know whether a single thread handles/handled your request? – marc_s Mar 01 '10 at 14:21
  • 2
    marc_s is correct. You should not depend on knowing this. Your code should continue to work even if they changed the thread agility tomorrow, then changed it back the next day. – John Saunders Mar 01 '10 at 14:24
  • @marc: 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? – Amitabh Mar 01 '10 at 14:56
  • @Amitabh: I quite honestly don't know whether anyone can guarantee that a WCF request will be handled by one thread only. However, if your solution seems to be working - let it be! Don't fix stuff that's not broken..... there's enough stuff that really need fixing ! Go fix that first..... – marc_s Mar 01 '10 at 16:28
  • @marc: There are some issues I am getting in Multi User Testing and wanted to know if using ThreadStatic can be a possible issue. – Amitabh Mar 01 '10 at 17:03
  • @Amitabh: in what cases do you need ThreadStatic variables? Couldn't you use e.g. just regular member variables on the service class, or handle that state you need and wish to keep in some other way? ThreadStatic is always a bit tricky.... – marc_s Mar 01 '10 at 17:24

1 Answers1

4

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

The above code snippet is from http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.concurrencymode.aspx

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.

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.

Community
  • 1
  • 1
Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • 1
    @Anders: +1 - excellent link - thanks! I was kinda leaning towards that, but couldn't back it up in any way, shape of form – marc_s Mar 01 '10 at 17:21