Tuesday, December 21, 2004

WebServices Series Part - 4 ( Sync & Async Communication )

we will checkout how to consume our web service using SOAP in Synchronous and Asynchronous modes.

To consume to our web service we need to create our web service client.

Let us go ahead and create our web service client.

Open a new windows application and Select References Right click and select Add Web Reference. Select our Trigonometry web service.

WebReference to this web service will be added and along with it you will notice the reference.map , .disco and .wsdl files will be added for the web service. Our client interacts with our web service using a proxy class.

To view the proxy class ... Select the solution in the Solution Explorer and click on Show All Files ... You will notice a file under the Reference map node called as Reference.cs.

Open the Reference.cs file and Checkout the BASE CLASS for our webservice proxy class, it should be SoapHttpClientProtocol.

You should also notice that for each method in our webservice there are three methods in the webservice proxy class.

For Example these are the the three methods for Sine.

public System.Double Sine(System.Double angel)
public System.IAsyncResult BeginSine(System.Double angel, System.AsyncCallback callback, object asyncState)
public System.Double EndSine(System.IAsyncResult asyncResult)

Out of these three the first one is used for Synchronous communication and latter two for Asyncrhomous communication.

Let us checkout Synchrounous Communication.

Add a Form and add a button and a label to that Form

Declare a class level variable for our webservice

localhost.Service1 trigoService = new localhost.Service1();

Write the following code in the button Click event

private void button1_Click(object sender, System.EventArgs e)
{
label1.Text = Convert.ToString(trigoService.Sine(90));
}

Execute the apllication and checkout the results.

One of methods of Asynchronous communication with web service is using Callbacks.

To start with declare a class level variable for IAyncResult

private IAsyncResult oIAsyncResult;

To consume our web service in asynchronous mode let us add another button to our form and add the below code. When we communicate with our web service in a asynchronous mode we will have to call the BeginSine method and pass a CALLBACK function to it. After our web service is done with its processing it will callback our function to return the result.

private void button2_Click(object sender, System.EventArgs e)
{
oIAsyncResult = trigoService.BeginSine(90,new AsyncCallback(CallBackForEndSine),null);
}

public void CallBackForEndSine(IAsyncResult oIAsyncResult)
{
double sineVal = trigoService.EndSine(oIAsyncResult);
label1.Text = Convert.ToString(sineVal);
}

Here I am calling the BeginSine method and passing it the reference of our CallBack method CallBackForEndSine. You should notice that on Calling the BeginSine method I am returned an IAysncResult object. This object acts as a TOKEN between the client and the webservice. Hence when the Webservice is through with its processing and Callback method is called, to get the result from the webserivce we need to call EndSine method in the CallBack method passing IAsyncResult Token.

The Difference between Asynchronous & Synchronous consumption of webservices can be noticed when there is a lot of processing done by a webservice. Thus if our webservice is consumed in a synchronous mode then our clients will have to wait for the processing to complete.

To get a good taste of Asynchronous processing with our webservice we will make a little change in our methods. Before returning the result we will make the thread sleep for 20 secs.


public double Sine(double angel)
{
Thread.Sleep(20000); // make a similar change in all the methods
return System.Math.Sin(angel);
}

Compile the webservice. Update the webservice reference in your Client project and Execute your Client.

Now if our web service is consumed synchrounsouly our client will have to wait for the result. Click on Button1 and check it out. The control will come back after 20 secs and till then your application will not respond to any other events. Now let us consume the webservice in an ansychronous mode by clicking on Button2. Since the communication is asynchronous the control will return back immediately rather than waiting for 20 seconds. Later the call back function will be called to deliver the results.

No comments: