2

I want to implement a WCF service that responds immediately to the caller, but queues up an asynchronous job to be handled later. What is the best way to go about doing this? I've read the MSDN article on how to implement an asynchronous service operation, but that solution seems to still require the task to finish before responding to the caller.

Kevin Pang
  • 41,172
  • 38
  • 121
  • 173
  • If it's a submit and forget operation spinning up a thread or delegate is probably what you want to do and run the operation while the Web Service request completes immediately. The question though is: Do you need to notify the client when the process is completed? That's when things get tricky because that requires service subscriptions or alternately client polling to check for job status. The following might help: http://stackoverflow.com/questions/1537302/subscribe-to-events-within-a-wcf-service – Rick Strahl Sep 21 '11 at 00:22
  • @RickStrahl I do need to notify the client when the process is complete. However, they have a set of WCF services of their own that I can post those results to. It sounds like using a delegate is the simplest way to go about this. – Kevin Pang Sep 21 '11 at 00:32

2 Answers2

1

Any WCF service can be made asynchronous -

One of the nice things about WCF is you can write a service synchronously. When you add a ServiceReference in the client, you have the option of generating asynchronous methods.

This will automatically make the service call asynchronous. The service will return when it's done, but the client will get two methods - BeginXXX and EndXXX, as well as XXXAsync + an XXXCompleted event, either of which allows for completely asynchronous operation.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • The problem here is that I don't control the client code. The client actually created the interface that my WCF service must adhere to and, from what I can tell, they simply want to send a message over to my WCF service and have it respond immediately. Then, when my server has successfully processed the initial request, they want it to send a separate request out to their end. Essentially, they want my WCF service to work asynchronously rather than change their client code to call my service asynchronously. – Kevin Pang Sep 21 '11 at 00:17
1

There are many ways to accomplish this depending what you want to do and what technologies you are using (e.g. Unless you are using silverlight, you may not need to have your app call the service asynchronously) The most straight forward way to achieve your goal would be to have your service method start up a thread to perform the bulk of the processing and return immediately.

Another would be to create some kind of request (e.g. Create an entry in a datastore of some kind) and return. Another process (e.g. A windows service, etc.) could then pick up the request and perform the processing.

Maciej
  • 2,175
  • 1
  • 18
  • 29
  • So you're suggesting I do something like create a delegate to handle all the heavy lifting and call BeginInvoke on it before sending back the initial response? – Kevin Pang Sep 21 '11 at 00:21
  • Yes. One of the thread's responsibilities would be to notify their end when it finishes processing the job. – Maciej Sep 21 '11 at 00:24
  • You may want to consider using a thread pool like indicated in [this post](http://stackoverflow.com/questions/2240251/wcf-is-it-safe-to-spawn-an-asynchronous-worker-thread-on-the-server) – Maciej Sep 21 '11 at 00:29