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 .
<o:p>
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..
Here we are going to use HelloWorldOneWayAsync because we are going to use the Fire and Forget mechanism.
Now I have done the Plumbing for the Events .you can see bellow
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
To mitigate this error we have to Set an attribute called Async=true in Code behind of the Default.aspx page
After putting this See what will happen Again Type 10000 in the Text box see how much milliseconds we have to wait for a response.
Woowww!!!!!See what happened it took only 55 milliseconds to execute. From this what we can interpret..?
Yess we can say that the Web method had invoked and the control is returned back to the Consuming application without waiting to complete the execution of the web method.
You might be thinking now like is there any need of the Web method decorated with HelloWorldOneWayAsync why don’t we straight away use HelloWorldOneWay by only providing the one way attribute on top..Will it work..? Yes offcourse it will.. to make a web method one way you just give an attribute
[SoapDocumentMethod(OneWay = true)] on top of the web method .there is no need of setting the service reference by checking the Async related check box and wire the events provided by doing so. Are you confused?…
" />
I would like to ask one question now … if we are using the fire and forget mechanism what will happen if the web service is down, how the consumer will know there is some error in web service and it is not called …Hope now you got the point. Right.?
For this purpose we are using the Async web method and the events to just want to know whether the web service method is called successfully without any error.
Then the Next Question would be.. How will we identify the error happened in the service or how will we identify the service call is success. You can see the answer here bellow.
Yes the Error Property of the AsyncCompletedEventArgs will give us the Error.
Let’s see what error we will get if the Web service is down. For this I have edited the name of Web service in the Service reference (Reference.cs) class to a wrong one and Executed the Application and I have put a break point in the consuming application and verified the e.Error, by doing this I got an error like
See Like this we can get the errors of the Services. The main purpose which I used is to identify whether the service is Up or Down.
Would you like to see the Difference in Execution time Of Synchronous Web method..?
For this I have created a new web method with the same Code without decorating with the one way attribute HelloWorldNotOneWay(int milliSeconds) as same as before.
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.
So this is all what I understood about one way Web service method .this might be use full for Developers
Thank you