Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Service which creates a new thread for every new request

Tags:

c#

.net

wcf

Is there a way to configure a WCF Service to create a new Thread to handle any new incoming request?

like image 573
Nazgul Avatar asked Sep 16 '09 05:09

Nazgul


2 Answers

Yes you can do that - it's called "per-call" handling of requests. The ServiceHost will create a new instance of your service class for each request coming in to handle that one request.

To do this, you need to set your Service class (the one implementing the service interface) to be "PerCall" - you do this by applying an attribute on your service class:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
public class YourService : IYourService
{
...
}

Marc

like image 135
marc_s Avatar answered Oct 14 '22 07:10

marc_s


Depends on what exactly you want, but the following service behaviour will solve it:

ServiceBehavior:
ConcurrencyMode=ConcurrencyMode.Multiple
InstanceContextMode=InstanceContextMode.Single

Your class will be a singleton, but all calls made to the methods will run in a separate thread. If you need any synchronization though you have to do it manually.

Also don't forget to look into throttling to be aware of potential performance issues.

like image 40
dr. evil Avatar answered Oct 14 '22 07:10

dr. evil