February 17, 2019
Hot Topics:

Asynchronous Web Services Invocation in .NET Framework 2.0

  • February 9, 2005
  • By Thiru Thangarathinam
  • Send Email »
  • More Articles »

Consume the Web Service Asynchronously from a Windows Forms Application

In this section, you create a Windows Forms application to consume the Web service. Create a new Visual C# Windows Forms application named AsyncWebServicesClient. Next, add a Web reference to the CategoriesService by selecting Project->Add Web Reference from the menu. In the Add Web Reference dialog box, enter the location of the Web service and hit GO. Then, specify the name of the Web reference as HelloWorldServiceProxy, as shown in Figure 4.

Click here for a larger image.

Figure 4. Specify the Name of the Web Reference as HelloWorldServiceProxy

Now you are ready to consume the Web service. To start, import the Web service proxy namespace at the top of the Form1:

using AsyncWebServicesClient.HelloWorldServiceProxy;

To the form, add a command button named btnInvokeHelloWorld and modify the click event of the command button to look as follows:

private void btnInvokeHelloWorld_Click(object sender, EventArgs e)
    HelloService service = new HelloService();
    //Hookup async event handler
    service.HelloWorldCompleted += new 

The above code shows the event handler HelloWorldCompleted (that is available through the proxy of the Web service) being hooked up to a local method named HelloWorldCompleted. The method named HelloWorldAsync provides a way to asynchronously invoke the HelloWorld method.

The new <MethodName>Async provides a seamless way to asynchronously consume the Web service from a client application. For example, if you have a Web service method named GetCategories and you want to leverage the asynchronous invocation framework, you need to do two things:

  • Create an event handler for the GetCategoriesCompleted method and hook it up to a local method that can process the results returned by the Web service.
  • Invoke the Web service method by calling the GetCategoriesAsync() method through the Web service proxy.

The local HelloWorldCompleted method is defined as follows:

void HelloWorldCompleted(object sender,
                         HelloWorldCompletedEventArgs args)
    //Display the return value

After the Web service has finished its execution and returned the results, the .NET Framework will automatically invoke the above HelloWorldCompleted method. As you can see, this method takes two arguments: an object of type "object" and a "HelloWorldCompletedEventArgs" object. You can capture the results returned by the Web service by using the Result property of the HelloWorldCompletedEventArgs object (as shown in the above code). In this example, the output the Web service returns is displayed in a message box.

Implement Data Binding with a Web Service

Apart from the new Web service asynchronous invocation framework, .NET Framework 2.0 also introduces a new feature through which you can perform data binding, with the results returned from the Web service in a seamless manner. This section demonstrates this feature with a practical example. It begins by creating the Web service.

Implementation of the Web service

To start, add a new Web service to the AsyncWebServices project by right-clicking on the project and selecting Add New Item from the context menu. In the Add New Item dialog box, select Web service from the list of templates and name the Web service CategoriesService.asmx. Modify the code-behind of the CategoriesService.asmx file to look as follows:

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1,
                   EmitConformanceClaims = true)]
public class CategoriesService : System.Web.Services.WebService
    public DataSet GetCategories()
            using (SqlConnection conn = new SqlConnection())
                string connectionString =
                conn.ConnectionString = connectionString;
                SqlCommand command = new SqlCommand
                    ("Select * from Categories", conn);
                command.CommandType = CommandType.Text;
                SqlDataAdapter adapter = new
                DataSet categories = new DataSet("Categories");
                return categories;
        catch (Exception ex)
            EventLog.WriteEntry("Application", ex.Message);
            throw ex;


Page 2 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date