WCF Multithreaded Self-Service

It seems that WCF uses only one thread when using its own hosting. I would like to use multiple threads or a thread pool for this. Is this possible with setting up my own hosting or do I need to use IIS for this?

+6
source share
3 answers

If you host the service yourself in a UI application with the default service behavior configured, you are likely to see the behavior you are describing. The default behavior of the service uses a synchronization context. If the service host starts in the user interface thread (WinForms, WPF), all requests are sent to the general Windows message loop => all requests are processed sequentially through the user interface thread.

In any other case (including manually configuring [ServiceBehavior(UseSynchronizationContext = false)] for services hosted in a user interface thread), the service node sends a new thread from the thread pool for each request. There are a few more differences based on instance context mode and concurrency mode, but with the default settings you will see the behavior that I described.

+10
source
 [ServiceBehavior(UseSynchronizationContext = false)] 

The problem is fixed. Tested this in a service and command line application after reading this comment and did not experience the same concurrency problem. Therefore, I will gladly confirm this only when creating the user interface instance.

I think the lesson learned is to use the command line application for the WCF test harness.

+1
source

I myself accept everything, ensuring that it is not single-threaded. The comment above is probably on the right track - make sure that if your ServiceBehavior attribute in the impl service is set to InstanceContextMode.Single, you also set ConcurrencyMode.Multiple, otherwise you will only see one thread. By default, if you do not have the ServiceBehavior attribute, you will receive one instance of the impl service per invocation (InstanceContextMode.PerCall, ConcurrencyMode.Single). It may also be related to throttling the connection, but presumably you know if you configured this in your configuration.

0
source

Source: https://habr.com/ru/post/886332/


All Articles