Start a Long Process from a Web Page Using XML Web Services
page 1 of 1
Published: 22 Jan 2004
Unedited - Community Contributed
Once again, XML Web Services come to the rescue with a simple solution to the common problem of web pages that want to do some background processing on the server and yet still return to the client without waiting for completion of the process.
by Steve Sharrock
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 40524/ 30

When writing web applications, I often encounter the situation where I would like to start a long process on the server during a post-back and yet return to the client before the process completes. The long process might typically be a lengthy database transaction, creating and sending to an email list, or perhaps some type of report generation.

In a Winform application, I would simply spin a new thread to perform the lengthy operation that would run in the background while my main UI thread would continue. In ASP.NET web applications this gets a bit more complicated than I wish to deal with from within a web page (see below for a threading solution). While it is certainly possible to start a server process to perform the operation, this is also more complex, and I've become very spoiled with the ease of creating web applications with Visual Studio.NET.

Once again, XML Web Services or, as I some times call it, "Poor Man's Remoting" comes to the rescue. The creation of a Web service is so simple with the Framework, especially using Visual Studio.NET, I should not have been surprised that turning a web service into a "fire-and-forget" process was so easy.

For those of you who have not yet created an XML Web Service, you should create a Visual Studio ASP.NET Web Service project. You'll get all of the code for a simple "Hello World" application generated for you. You'll notice that just about the only thing that makes the underlying code-behind different from ASPX web pages, is that each method of the Web service is decorated with the [WebMethod] attribute.

public string HelloWorld()

{ return "Hello World"; }

When you call a Web service method of this type, the HTTP connection is held open until the WebMethod returns its value to the caller. In the HelloWorld example, the value must be returned, and the processing time is trivial. However, if the WebMethod was a long operation, and there were no return value, it would be desirable to start the method processing and immediately return to the caller in a style similar to a "fire-and-forget" process.

While it's not really fire and forget, the net effect is about the same. By decorating the method with the [SoapDocumentMethod(OneWay=true)] attribute, the server holds the HTTP connection open while it loads and parses the request message, but then returns immediately to the caller with an HTTP 202 response, indicating it has begun processing the request.

public void HelloWorld()

{ the long process... }

Voila. I can now include a web service in the form of an ASMX page in my web application that will perform the long operation. By invoking this web service from the post-back server code of my web application, I can return to the client and the process will continue until completion, letting IIS do all of the multitasking/threading work for me.

Threading Solution

For the more adventurous, there is great example on the MSDN site that illustrates a threading solution: How To: Execute a Long-Running Task in a Web Application


There are, of course, several restrictions. For instance, the web service method must return void, meaning there can be no return value, and no direct way of determining when, and if, the long operation completed. But still, this once again illustrates how the .NET Framework, especially when using Visual Studio.NET, provides solutions that require very little programming to achieve results that in the past would have represented a major development effort.

Note: By default, SOAP messages are in the "document" style of formatting. If you've been using Remote Procedure Call (RPC) formatting for the messages, then you should use [SoapRpcMethod(OneWay=true)], rather than [SoapDocumentMethod(OneWay=true)].

Reference: There is a very good article on this and other web service attributes available at:,2000042147,20270876,00.htm


User Comments

Title: What if i need to return something   
Name: Jorge Ramirez
Date: 2011-11-28 2:49:11 PM
Hi, i am looking for somthing like this.. i need to perform a long task into the WebServer.. but (while the process is running in background) i need to return an Id to the user. Something like "Thanks, Your process is running with Id: {0}".
Title: Problem   
Name: Arian
Date: 2008-07-14 10:43:40 AM
I have same problem :

Exception Details: System.Net.WebException: The request failed with an empty response.

any body get same error?

It works on local mchine, my web services return a simple dataset like this:

public DataSet GetCategoryInfo(int culturInfoId)
int culturId;
if (culturInfoId != 1 || culturInfoId != 2)
culturId = 1;
culturId = culturInfoId;
ServiceProviderBLL sb = new ServiceProviderBLL();
CategoriesDataTable = sb.ServicesGetCategoryDataTable(culturId);
DataSet IntakeDataSet = new DataSet("IntakeDataSet");
return IntakeDataSet;
Title: webservices   
Name: Vanea
Date: 2008-06-23 7:01:52 AM
I am getting following error:
client found response content type of ''.but expected 'text/xml'.The request failed with an empty response
anybody has similar problem!"
i am getting this problem too ...
Title: How too   
Name: Tim
Date: 2008-02-21 1:40:24 PM
Make sure the function in the web service is a void function or a Sub in VB. IT can't have a return type.
Title: webservices   
Name: Al
Date: 2006-04-22 11:04:45 PM
I am getting following error:
client found response content type of ''.but expected 'text/xml'.The request failed with an empty response
anybody has similar problem!
Title: Excellent Article   
Name: ionian
Date: 2006-01-19 12:54:19 PM
Thanks, very helpful article. You can see another example at the MSDN Website at:
Title: Thanks for article   
Name: Charlie
Date: 2005-09-16 4:01:15 PM
Thanks for the insight. I almost wasted a ton of time when this approach works well and is very easy to implement.
Title: progger   
Name: Stephan
Date: 2004-10-11 1:51:16 PM
thanks a lot, this will help me
Title: coder   
Name: Guoqi
Date: 2004-10-02 2:31:40 AM
Thanks for it. XML web service is what I am looking for.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2016  |  Page Processed at 2016-10-28 8:11:10 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search