Aaron Anodide
Aaron Anodide

Reputation: 17180

How to do async work as part of web service?

I'm coding web service A and calling web service B.

Once I validate the input to A I want to return a success code and then invoke B.

Ways I'm considering:

The runtime context is a Service.svc hosted by an ASP.NET web app.

How should I do this?

Update: I just want to say that the answers were all useful. I decided to mark the one that I used.

Upvotes: 2

Views: 739

Answers (4)

Stephen Cleary
Stephen Cleary

Reputation: 456322

Once I validate the input to A I want to return a success code and then invoke B.

In this case you should place the work into a persistent queue (MSMQ, Azure Bus, etc), and only then return a success code. Another system (not hosted in ASP.NET; i.e., Azure worker role, Win32 service, etc) should retrieve work from the queue and invoke B.

This is the only reliable way to do it. Phil Haack has a good blog post on why it's a bad idea to do both parts in ASP.NET; it comes down to recycling. More info on recycle triggers here and here.

Upvotes: 2

Paul Smith
Paul Smith

Reputation: 3216

The question isn't really specific to any particular service interface. Generally, your decision depends on why you want to invoke something asynchronously:

  1. If you just want to give the client a quick response to a single long-running method call, invoke your async method and immediately return a token (task ID or the like) that can be used to check on the task's progress later.
  2. If you have several long-running methods to execute and you mainly want to invoke asynchronously to gain efficiency through parallelism, invoke anything you can at the outset & continue with everything else that doesn't depend on a previous method. Await any tasks that are needed for your return result to be complete, or for a subsequent task to start.
  3. In most of these situations, I would give the client the choice between a synchronous and asynchronous method with a status token; if they want to continue on they can check back later, and if it's important to get that result before continuing, well that's an option too.

The approach to these gets easier with each subsequent framework release > 3.5, and I trust that other answers have given you good insights into the specifics of how to kick off, move on and/or wait for asynchronous task results in c#.

Upvotes: 1

John Saunders
John Saunders

Reputation: 161773

Your second idea is a good one. Send the async part to a Windows Service. If you need the reliability, then do it using a message queue. In any case, you can use WCF to send the request to the other service.

Upvotes: 1

Greg
Greg

Reputation: 11480

This link over at Code Project should give you all the information required to create a stable Async Communication with Windows Communication Foundation.

A book with a lot of handling of this form of communication:

  • WCF Step by Step
  • WCF In a Multi-Tier Framework

Hopefully that helps. I'd give you some examples but that tutorial on Code Project should provide just what you need.

Upvotes: 2

Related Questions