Working with concurrent requests in the WCF native web service

I am starting to develop the following scenario (Visual Studio 2010, WCF): different clients on our network will call a web service that I developed myself, which is hosted as part of a Windows service, so we are talking about a self-hosted WCF web service in a Windows service.

The WCF stand-alone web service, in turn, makes calls to an external web service, and therefore acts as a kind of facade.

Obviously, my WCF web service itself must accept simultaneous calls, and here my question arises: what do I plan for operations that call from different clients on my own WCF web service, I would just create a client for an external web service, makes a call to the external web service, receives a synchronous response back from the external web service, and then answers the calling client.

It sounds simple, but is there anything I need to consider the multiple threads entering my service node? Obviously, I need to be careful with any "global" variables in the host itself, but I need to save it if I just create external web service clients in different operations of the called web service or am I missing something?

Thanks everyone!

Cheers, Stevo

+4
source share
1 answer

Here you have several options, especially in that they are related to installing WCF and Concurrency.

Instancing: your choice here

  • PerCall : for each client request, a new InstanceContext instance is created (and therefore a service object).
  • PerSession : for each new client session, a new InstanceContext instance is created (and therefore a service object), which is maintained for the lifetime of this session (this requires a binding that supports sessions).
  • Single . A single InstanceContext instance (and therefore a service object) processes all client requests for the lifetime of the application.

Concurrency:

  • Single . Each instance context is allowed to have at most one message flow in the instance context at a time. Other topics that wish to use the same instance context should be blocked until the source thread exits the instance context.
  • A few . Each service instance can process messages from multiple threads simultaneously. The service implementation must be thread safe to use this concurrency mode.
  • Reentrant . Each service instance processes one message at a time, but receives calls to re-login operations. The service accepts these calls only when called through a WCF client object.

Let's first look at Instancing:

For the call . Basically what happens here is what happens if two clients make a call to your service, 2 different instances of your service are created and then destroyed when the call ends

Per session . If these two clients make 2 calls through your proxy server, then the wcf instance created on your host for both of these calls will be the same (for each proxy server, so there are 2 instances serving 2 each, except for each caller, goes to the same instance), and then destroyed

Single . Both proxies / clients will use the same instance, so caller 1 calls the operation and then caller 2 makes the call, the same instance will be reused.

Pretty straight forward.

As for concurrency, its number of threads is active in the above instance context at a time. Here I am a proponent of experience and MSDN, which states that "understanding and developing code that safely uses more than one thread can be difficult to write" ( but not impossible )

Thus, based on your requirements, it seems that you do not want to maintain state and scalability, it can be a problem, since you handle several proxy calls, and you do not need to transfer any global data that requires Single Instance mode, then the most the likely solution would be:

[ServiceBehavior (ConcurrencyMode.Single, InstanceContextMode=InstanceContextMode.PerCall)] public class YourService: IYourService { } 
+10
source

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


All Articles