简体   繁体   中英

How to make a call to WCF webservice with more than one client at the same time (in parallel)

I have ac# WCF web service which is a server and I do have 2 clients one is java client and another is c++ client. I want both the clients to run at the same time. The scenario I have and am unable to figure it out is:

My java client will be making a call to the WCF web service and the processing time might take around 10 mins, meanwhile I want my c++ client to make a call to the web service and the get the response back. But right now I am just able to make a call to web service using c++ client when the java client request is being processed. I am not getting the response back for c++ client request until java client request is completed.

Can any one please suggest me how to make this work parallel. Thanks in advance.

Any "normal" WCF service can most definitely handle more than one client request at any given time.

It all depends on your settings for InstanceContextMode :

  • PerSession means, each session gets a copy of the service class to handle a number of requests (from that same client)

  • PerCall means, each request gets a fresh copy of the service class to handle the request (and it's disposed again after handling the call)

  • Single means, you have a singleton - just one copy of your service class.

If you have a singleton - you need to ask yourself: why? By default, PerCall is the recommended setting, and that should easily support quite a few requests at once.

See Understanding Instance Context Mode for a more thorough explanation.

Use

[ServiceBehavior( ConcurrencyMode = ConcurrencyMode.Multiple )]

attribute over your service class. More on this for example here:

http://www.codeproject.com/Articles/89858/WCF-Concurrency-Single-Multiple-and-Reentrant-and

This is peripheral to your question but have you considered asynchronous callbacks from the method that takes 10+ minutes to return, and then having the process run in a separate thread? It's not really good practice to have a service call waiting 10 minutes synchronously, and might solve your problem, although the service should allow for multiple callers at once anyway (our WCF service takes thousands of simultaneous requests).

When you call a WCF you have a choice in either calling it synchronously or asynchronously. A synchronous call waits for the response to send back to the caller in the same operation. In the caller it would look like "myresult = svc.DoSomething()". With an asynchronous call, the caller gives the service a function to call when it completes but does not wait for the response. The caller doesn't block while waiting for the response and goes about its business.

Your callback will take DoSomethingCompletedEventArgs: void myCallback(object sender, DoSomethingCompletedEventArgs e) { var myResult = e.Result; //then use the result however you would have before. }

You register the callback function like an event handler: svc.DoSomethingCompleted+=myCallback; then svc.DoSomethingAsync(). Note there is no returned value in that statement; The service would execute myCallBack when it completes and pass the result. (All WCF calls from Silverlight have to be asynchronous but for other clients this restriction isn't there).

Here's a codeproject article that demonstrates a slightly different way in detail. http://www.codeproject.com/Articles/91528/How-to-Call-WCF-Services-Synchronously-and-Asynchr

This keeps the client from blocking during the 10+ minute process but doesn't really change the way the service itself functions.

Now the second part of what I was mentioning was firing off the 10+ minute process in a separate thread from inside the service. The service methods themselves should be very thin and just be calling functionality in other libraries. Functions that are going to take a long time should ideally be called in their own threads (say a backgroundworker, for which you register on the service side a callback when it completes) and have some sort of persistent system to keep track of their progress and any results that need to go back to the client. If it were me I would register the request for the process in a db and then update that db with its completion. The client would then periodically initiate a simple poll to see if the process was completed and get any results. You might be able to set up duplex binding to get notified when the process completes automatically but to be honest it's been a few years since I've done any duplex binding so I don't remember exactly how it works.

These topics are really too big for me to go into depth here. I would suggest researching multithreaded operations with the BackgroundWorker.

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