简体   繁体   中英

WCF Service which creates a new thread for every new request

有没有办法配置WCF服务来创建一个新的线程来处理任何新的传入请求?

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

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.

No, because you would never want to do this. What are you really trying to achieve?

EDIT

Based on more info coming in, here's what I think.

If you just want "sticky state" per request, you should use the state on the Instance and use InstanceContextMode.PerCall, as per marc_s's response.

If you need some state to be in thread-local storage for your call, you can consider using ICallContextInitializer as a way to marshal the state over to the thread that WCF chooses to invoke your method on (and clean the thread state when the call finishes).

But you should not care about "which thread". WCF will handle that with a thread pool on your behalf.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM