Fire and Forget Web Method in ASP.Net
Document describes about One way web Method creation and consumption in Traditional ASP.net Web Service.
Introduction:
In many cases We might have encountered some situation Like, We would like to call a web method from our Consuming application but we don’t want to wait until the request get processed or else, we are not excepting any return from the particular web method. Just as we say Fire and forget.
E.g.:- We have got a Client Web application which consumes a web service . This particular web method is used by the web application to push a bulk of record from some file to a data base. And not the consuming application excepts a result back only the consuming application just want to know that whether the web method has been called successfully or not .after calling this the web application will carry forward with its own processing without waiting the web method call to complete its processing .
How can we achieve this:
To achieve the above mentioned functionality, while creating a web method in asp.net web service we have to decorate the Web method with an attribute [SoapDocumentMethod(OneWay = true)]. Do you think that this will satisfy the above requirement..? it will not.. To achieve this we have to set the proxy class of the Web service in such a way that it will accommodate the One way Asynchronous mechanism.. How can we do this..? Will explain in next section with example.Not only in the proxy and the web method we have to modify but also in the Consuming client we have to do supporting plumbing to make this happen.
Sample Project : Here am going to show this through a sample project in step by step manner.1) Create a Web service decorated with the SoapDocumentMethod One-way set to true, which accepts an int param, and tells the thread to wait for some milliseconds to give a wait time to show some thing is happening, or else you can have your own code which will take some time to complete the execution. Here We completed the Work of the web method nothing other than this has to be done in this .and one important thing you have to notice is the return type of the web method which is set to void here.
2) Create a Consuming application in the same solution (for ease in the same solution).
Here we go.. We have created a Web service and a consumer .Our next task is to do the plumbing for the one way method consumption, for that to consume the web method we have to create a proxy in our consuming web application .
3) Creating Proxy through adding Web reference is mentioned bellow step by step .
We have added the Web Reference successfully in our consuming application. You can see in the bellow shown snap shot.
As of now everything is well and good right..?Now we are going to do the plumbing in our consuming application. See
For this I am going to create an object of the web service in our consuming web application .on looking at the intellisense which we are getting from the ,it will display 2 Web methods but we only created one right .?.
Look at the bellow snap shot in which all web methods are displayed.

Yes you can see our HelloWorldOneWay ,HelloWorldOneWayAsync and One Event called HellowWorldOneWayCompleated..


What I have done is created proxy object .then I have wired the HellowWorldOnewayCompleated Event generated with an event handler. You can see the entire code snippet bellow.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
namespace OnewayConsumer
{
public partial class _Default : System.Web.UI.Page
{
MyOneWayService.OneWayWebService objProxy = null;
string strTimeElapsed = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnClick_Click(object sender, EventArgs e)
{
Stopwatch objStopWatch = new Stopwatch();
objProxy = new MyOneWayService.OneWayWebService();
objProxy.HelloWorldOneWayCompleted += new MyOneWayService.HelloWorldOneWayCompletedEventHandler(objProxy_HelloWorldOneWayCompleted);
objStopWatch.Reset();
objStopWatch.Start();
objProxy.HelloWorldOneWayAsync(Convert.ToInt32(txtWaitTime.Text.Trim()));
strTimeElapsed = objStopWatch.ElapsedMilliseconds.ToString();
}
void objProxy_HelloWorldOneWayCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
lblResult.Text = "Fired and Forgot.....!!!!! Took ' "+strTimeElapsed+" 'to Complete Execution";
}
}
}
Now run the application …. Our page will looks like bellow
In this page what we have to do is Enter some integer value in the text box and wait for the response to come ..
What we expect is if we are entering 10000 milliseconds it as per our web service method it will take 10000 milliseconds to complete the execution and return back. But what we have done is just fire the web service method and come back to the application without waiting the web method complete the execution..Right..?
As a first step go and execute the application
Enter 10000 in the text box the hit GO see what will happen…….
The result would be an error like





Yes the Error Property of the AsyncCompletedEventArgs will give us the Error.


And here aim going to call the newly created web method from another button click

See the difference in Execution Time and the time took to return Back the Control.
