I need some help on consuming windows service from a WCF service using multi-threading.
We have to implement a windows service in a secured corporate network which should pick requests synchronously from a WCF service hosted in internet. The windows service will process the request and send back a file to the WCF service.
Normally the windows service will process each request 1-2 mins approx and there can be multiple requests generated by the WCF service at same time.
I would like to know the best approach to implement this scenario or is there an already existing/proven solution which gives the best performance.
Would appreciate your valuable suggestions.
We use Visual studio 2010 and c# for development.
Your choice in this senario are far and many when using WCF but to answers your question, from my perspective.
Firstly, to do this synchronously would be a bad choice as it will lock both the WCF service and the Windows Service until each file finished processing.
It really is dependant on whether you control both the Windows Service and WCF Service
If you have control of the WCF service on the internet I would create MSMQ on the Windows Service side which the WCF service would send requests to. Here is an article on how to do this over Http:
The Windows Service would then pole the MSMQ rather than the WCF service. If your Windows Service is inundated with messages, the messages will remain on the MSMQ until Windows Service is ready to process them.
If possible, I would also have the Windows Service sending processed files to MSMQ on the WCF side and have the WCF service picking the processed requests from MSMQ. If it is not possible to do so on the WCF serivce side then have an operation which accepts the processed file as a parameter.
WCF provides so many combinations and flexibilty that you can practically do what you want.
I'm not claiming that this is the right answer but this is the senario I would pursue myself.
- Edited by Dragan Radovac Saturday, March 10, 2012 12:01 AM
I am not sure how your WCF service is going to communicate with Windows Service? Do you mean your window service is hosting another WCF service which receive request from your internet WCF service?
If it is true,
WCF itself do concurrent request processing by setting ConcurrencyMode<//span>=Multiple and throttling behavior to manage server load and resource consumption (with the following properties):
- MaxConcurrentCalls. Limits the number of concurrent requests that can be processed by all service instances. The default value is 16.
- MaxConcurrentInstances. Limits the number of service instances that can be allocated at a given time. For PerCall services, this setting matches the number of concurrent calls. For PerSession services, this setting matches the number of active session instances. This setting doesn t matter for Single instancing mode, because only one instance is ever created. The default value for this setting is 2,147,483,647.
- MaxConcurrentSessions. Limits the number of active sessions allowed for the service. This includes application sessions, transport sessions (for TCP and named pipes, for example), reliable sessions, and secure sessions. The default value is 10.
So if you set MaxConcurrentCalls=10 than 10 threads would process10 request (10 files) in parallel by abstracting all multi-threading code.
Of course there are many other many options like MQ , Async you can think of.
- Edited by Lingaraj Mishra Saturday, March 10, 2012 5:27 AM
If the WCF service receives a request to process a file then there is no reason why the WCF service cannot send that request off to the windows service for processing through a MSMQ. The reason why I recommend a MSMQ is for durability. If the windows service goes down or you need to restart the windows service then all request will wait in the queue until the windows service is back online. If it is at all possible for the WCF Service to also receive messages or in your instance files from the windows service from a MSMQ then that would be great too.
I can see your dilemma where the client must wait for a response from the service and this could be most difficult if you are not implementing a Duplex Contract between the client and the WCF Service. If it is possible for you to implement a Duplex Contract between the client and the WCF service then the suggestion would work well as the WCF Service could callback into the client when the file is ready.
Another option would be for the client to submit a request and then poll the service every minute or so to determine whether the file is ready to be picked up. If you know that the processing is going to take over a minute on every request this could work well.
In regards to Instancing and Concurrency I'm a bit rusty and I should probably put some effort in this evening brushing up on the topics.
I find these videos to be great and if you have sometime maybe you should have a look also:
Thanks for the quick response Rupex & Lingaraj
We already use asynchronous method with MSMQ in other application. But this one we would like to do it in synchronous way.
Probably the Windows service is not going to to take 1-2 mins as i said earlier. Normally it take few seconds only max of 5 sec which we think may not have serious lock issues.
There is only one WCF from internet which needs to have two way communication with Windows service in Secured Network.
I am also working on to establish inter process communication between win Service (server) and WCF (client) mostly with Named Pipe as it supports multi threading.
Would appreciate if anyone can suggest any synchronous method of communication.