views:

37

answers:

2

Is there a way to have a web service (WCF/ASMX/etc) call do some processing after it returns it's results? Note I'm not looking for a asynchronous calls as the client needs the result back ASAP, before the the extra processing is done.

The primary constraint is that as far as I can tell, the only way for me to run code on my server is via ASP pages or Web Services.

Can this be done by spawning a background thread?

+1  A: 

You want a side effect which the client won't know about or control the success of? That is asynchronous processing, even if you return an immediate synchronous answer. If the results are observable and of interest to the client, you should have some way for clients and operators to determine whether it succeeded afterwards.

This hinges on how you host your web services (which is orthogonal from WCF providing a communications and activation layer). Normal web service hosting won't allow you process things outside the WS calls, mostly to prevent you from shooting yourself in the foot and eating all server resources.

  • Under ASP.NET ASMX hosting, you can enter another task to the thread pool: however, this may be terminated without warning if server loads are excessive. We had to resort to this in a production service recently: luckily, the server host doesn't experience high loads. We're somewhat nervous about the reliability of this solution.
  • I haven't worked with Windows Activation Services, but there should be a way of spawning additional work in a separate thread.
  • You can always implement your own worker process, e.g. as a Windows Service, and submit work to it as needed. This is not extremely difficult, but requires a bit of work to do reliably and scalably. Not a favourite, and the kind of scenario WAS is intended to address.

Spawning another thread is easy, but isn't something IIS should permit you to do freely, especially if it's running in any sort of multi-application web hotel.

Pontus Gagge
+1  A: 

Well, your web service will most likely be hosted on a Windows server, right? You could do any number of things:

  • launch a Windows workflow
  • write an entry to a database table, which then gets scanned by another app / process that then does something based on the entry (e.g. sends email etc.)
  • kick off a console app
  • call another service on the same or a different server

I think your options are really quite endless. However - they're not directly part of the webservice per se - you'd have to kick them off before the web service call ends. There's really nothing in either ASMX or WCF which allows you to "send request and then continue executing" - your service method send the response when it is done.

Marc

marc_s
I like that last one: call an async service that never returns anything to me.
BCS