Using a thread pool in a .NET REST service implementation

I am implementing my first REST service in .NET 4, and I came across something unexpected. It seems that I do not understand the underlining of the Microsoft ServiceModel, but could not find the answer in the traditional way.

To implement my web service, I followed the steps in this tutorial: http://blogs.msdn.com/b/endpoint/archive/2010/01/06/introducing-wcf-webhttp-services-in-net-4 .aspx

The service is running. I was surprised that Application_BeginRequest and Application_EndRequest in Global.asax are called by different threads. Looking at the stack trace, it seems that these threads are based on some thread pool.

Without any refactoring, this is a problem for us, since we always assumed that one request will always work in one thread, because of which we save some variables stored in the local stream storage. Variables are initialized in Application_BeginRequest and released in Application_EndRequest. It seems like with ServiceModel this is the wrong approach.

My questions:

  • Can I make any assumptions about which threads run my code when I use the ServiceModel?
  • Is there a way to limit execution to a single thread? Will it be bad for any reason?
  • What is the correct way to store a variable during a request when using ServiceModel?

Thanks.

+7
source share
2 answers

One thing I propose is to consider using WCF hooks rather than the Application_BeginRequest and Application_EndRequest methods. Four examples, here are four of the more useful hooks:

AfterReceiveRequest -> BeforeCall -> method call -> AfterCall -> BeforeSendReply

There hooks are quite powerful. You check the parameters before calling the method (centralize some records in one place) and do all sorts of other useful things. These are not the only hooks available, there are others that I use. For example, GetInstance allows me to override the creation of a service class object (so you can use dependency input frameworks, etc.).

When I use the mode for each concurrency call, these hooks, as well as the ALL method call, are called on the same thread. Hope this helps. I can provide links to implement these interceptors if you like.

Greetings

+2
source

You can see the [ServiceBehavior] attribute in your service implementation as it supports arguments to control how many instances are created and which thread model is used.

http://msdn.microsoft.com/en-us/library/cc681240.aspx

If you

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)] public class MyService : IMyService 

your service will work as a singleton, but with several threads - up to the threshold set in the WCF configuration to call your methods. To make it work on only one thread and thus serialize incoming requests, set ConcurrencyMode.Single.

Alternatively, you can deploy a new instance of your service for each call:

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)] public class MyService : IMyService 

An instance will have access to it with only one thread. In fact, when you have InstanceContextMode.PerCall, then ConcurrencyMode is ignored because it is always "Single" and each instance is running in its own thread.

0
source

All Articles