Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with concurrent requests in self hosted WCF web service

I am starting to develop the following scenario (Visual Studio 2010, WCF): Different clients in 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 inside a Windows Service.

That self hosted WCF Web Service will in turn make calls to an external Web Service, so is acting like a sort of facade.

Obviously my self hosted WCF Web Service will need to accept concurrent calls and here comes my question: What I am planning to is in the operations that are called from the different clients on my self hosted WCF Web Service, I would simply create the client for the external webservice, make the call to the external webservice, get a syncronous response back from the external web service and then reply to the calling client.

Sounds simple, but is there anything I need to consider about multiple threads entering my service host? Obviously I need to be careful with any "global" variables in the host itself, but I should be save if I simply create the external web service clients in the different operations of the called web service, or have I overlooked something?

Thanks all!

Cheers, Stevo

like image 270
ManOnAMission Avatar asked Dec 26 '22 18:12

ManOnAMission


1 Answers

You have a few choices here, particularly as they relate to WCF instancing and Concurrency.

Instancing: Your choices here are

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

Concurrency:

  • Single: Each instance context is allowed to have a maximum of one thread processing messages in the instance context at a time. Other threads wishing to use the same instance context must block until the original thread exits the instance context
  • Multiple: Each service instance can have multiple threads processing messages concurrently. The service implementation must be thread-safe to use this concurrency mode.
  • Reentrant: Each service instance processes one message at a time, but accepts re-entrant operation calls. The service only accepts these calls when it is calling out through a WCF client object.

Lets look at Instancing first:

Per Call: Basically here what happens is if two clients make a call to your service, 2 different instances of your service are created, and then destroyed once the call is complete

Per Session: If those two clients make 2 calls each via your proxy, the wcf instance created on your host for both of those calls will be the same (for each proxy so 2 instances serving 2 each but each caller goes to the same instance), and then destroyed

Single: Both proxies/clients will use the same instance, so caller 1 invokes an operation, and then caller 2 makes a call, this same instance will be reused.

Pretty straight forward.

As far as concurrency goes, its the number of threads active in the above mentioned instance context at one time. Here i side with 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)

So based on your requirements it seems you don't want to maintain state and scalability could be concern since you are servicing multiple proxy calls and you don't need to share any kind of global data that would necessitate Single Instance mode, then the most likely solution would be:

[ServiceBehavior
(ConcurrencyMode.Single,
 InstanceContextMode=InstanceContextMode.PerCall)] 
public class YourService: IYourService
{
}
like image 130
Ta01 Avatar answered Jan 13 '23 12:01

Ta01