May 21, 2019
Hot Topics:

Asynchronous Web Services Invocation in .NET Framework 2.0

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

Line-by-Line Code Breakdown

The above code first encloses the SqlConnection object within the scope of a using block. Then, it retrieves the connection string from the ConnectionStrings section of the web.config file:

string connectionString = System.Configuration.ConfigurationManager.

The code stores the connection string directly under the root <configuration> element in the web.config as follows:

    <add name="northwindConnectionString"

Then, it sets the retrieved connection string to the ConnectionString property of the SqlConnection object. After that, the code creates an instance of the SqlCommand object, passing to its constructor the SQL statement to be executed and the SqlConnection object as arguments. Then, it sets the CommandType property of the SqlCommand object to CommandType.Text to indicate that it wants to execute a SQL statement through the SqlCommand object:

SqlCommand command = new SqlCommand("Select * from Categories", conn);
command.CommandType = CommandType.Text;

Next, it creates an instance of the SqlDataAdapter and passes in the SqlCommand object to its constructor:

SqlDataAdapter adapter = new SqlDataAdapter(command);

After that, the code creates a DataSet object and then supplies the DataSet object as an argument to the Fill method of the SqlDataAdapter object:

DataSet categories = new DataSet("Categories");

Finally, it returns the DataSet object back to the caller using the return statement:

return categories;

Now that you have seen the implementation of the Web service, take a look at the client application that will consume the Web service.

Consume the Web Service from the Client

Open the previously created AsyncWebServicesClient project from Visual Studio 2005. 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 specify the name of the Web reference as CategoriesServiceProxy. Once the Web reference is created, you can import the namespace of the proxy using the following line:

using AsyncWebServicesClient.CategoriesServiceProxy;

Next, open Form1 and drag and drop a DataGrid control onto the Form designer. Add another command button named btnInvokeCategories to the form and modify the code in the Click event to look like the following:

private void btnInvokeCategories_Click(object sender, EventArgs e)
    CategoriesService service = new CategoriesService();
    service.GetCategoriesCompleted += new

The implementation of the GetCategoriesCompleted method is as follows:

void GetCategoriesCompleted(object sender,
                            GetCategoriesCompletedEventArgs args)
    //Bind returned results to the UI data grid
    dataGrid1.DataSource = args.Result;

As you can see, the DataSet returned from the Web service is directly bound to a DataGrid control and displayed on the form. If you run the form and click on the Invoke Categories Service button, you will see the output shown in Figure 5.

Click here for a larger image.

Figure 5. Run the Form and Click on the Invoke Categories Service Button

As Figure 5 shows, the categories information that the Web service returns is directly data bound to the DataGrid control.

Increase Developer Productivity, Enhance User Experience

The new asynchronous invocation framework that .NET Framework 2.0 introduced is an extremely useful feature that can go a long way toward increasing developer productivity. You can leverage the framework to increase the responsiveness of a .NET application, which greatly enhances the user experience as well.

This tutorial provided a thorough discussion of the asynchronous Web service invocation features of .NET Framework 2.0 and provided examples. It also showed how to perform data binding with the results returned from a Web service. Although the application you created was simple in functionality, it should provide a solid foundation for understanding how to easily implement the asynchronous Web service invocation feature in your .NET applications.

Download the Code

To download the accompanying source code for the demo, click here.

About the Author

Thiru Thangarathinam has six years of experience in architecting, designing, developing, and implementing applications using object-oriented application development methodologies. He also possesses a thorough understanding of the software life cycle (design, development, and testing). He holds several certifications, including MCAD for .NET, MCSD, and MCP. Thiru is an expert with ASP.NET, .NET Framework, Visual C# .NET, Visual Basic .NET, ADO.NET, XML Web services, and .NET Remoting. Thiru also has authored numerous books and articles. Contact him at thiruthangarathinam@yahoo.com.

Page 3 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