pavanarya – Pavan Kumar Aryasomayajulu

Contact Email : pavan.aryasomayajulu@gmail.com

Calling a webservice Asynchronously

leave a comment »

Hi in the post i want to write about calling web services asynchronously.

Actually when we call webservices using jquery or other stuff they will provide us with an option of asynchronous calling. Now let us see how to call a web service from console application asynchronously.

Creating our WebService

Hi first of all i am trying to create a simple plain web service. I am trying to create delay by making use of Thread.Sleep(10000)

WebService1.asmx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Threading;

namespace CallingWebServiceAsync
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            Thread.Sleep(1000);
            return "Hello World";
        }
    }
}

We are done with the creation of WebService now let us create a console application and this web service as a reference.

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Test.WebService1 s = new Test.WebService1();
            s.HelloWorldCompleted += new Test.HelloWorldCompletedEventHandler(Pavan);
            s.HelloWorldAsync();
            int i = 10;
            Thread.Sleep(2000);
        }

        public static void ExecuteOnCompletion(object obj,Test.HelloWorldCompletedEventArgs args)
        {
            Console.WriteLine("WebService execution completed");
        }
    }
}

1.In the first step we are creating an object of web service.
2. We have an event that is fired when the webservice execution is completed. In our example the event is HelloWorldCompleted(that is nameofservice+”Completed”).

3.Initialize the event with the delegate as follows

            s.HelloWorldCompleted += new Test.HelloWorldCompletedEventHandler(ExecuteOnCompletion);

4. ExecuteOnCompletion is the method that is supposed to be executed after webservice execution is completed.

5. ExecuteOnCompletion should have two parameters
i. an object type
ii. an object of HelloWorldCompletedEventArgs

public static void ExecuteOnCompletion(object obj,Test.HelloWorldCompletedEventArgs args)
        {
            Console.WriteLine("WebService execution completed");
        }

So what we can observe here is after executing this statement s.HelloWorldAsync(); the flows continues normally and after the webservice execution is complete then ExecuteOnCompletion() will be executed asynchronously.

But if we use s.HelloWorld() the execution stops at this line until the web service execution is complete.

So this asynchronous calling is useful when the web service execution takes much time and we are not depending on the webservice result immediately.

Thanks,
Pavan

Written by pavanarya

July 8, 2012 at 10:28 am

Posted in c#, WebServices

Leave a comment