Creating the Web Service Consumer

In this article I will explain creating the Web Service Consumer.

This article has been excerpted from book "A Programmer's Guide to ADO.NET in C#".

In my earlier article I created a Web service, you should create a client to use it. In this tutorial you'll create an ASP.NET Web form application that accesses the web service to retrieve an order. The client will then display the order in a DataGrid.

First, create an ASP.NET Web Application template, as shown in figure 8-13. As you can see in figure 8-13, I'm creating my application on the MCB Web server. Your default server may be localhost, so you can keep your server name as localhost and type your project name after the server.

Figure-8.13.jpg

Figure 8-13. Creating a new Web-based client application

Creating a web application adds a web page to the project, which is the default gateway when VS.NET is done creating the project. You can use Web pages as windows forms. You can add web and HTML controls to Web pages by simply dragging controls from the toolbox to the Web page.

In my sample, I add a label, a text box, a button, and a data grid by dragging them from ToolBox > Web Forms to the webform1.aspx Design View. Next, set the controls' properties. Figure 8-14 shows the final page after setting the properties.

Figure-8.14.jpg

Figure 8-14. Design view for searching for and displaying an order

To use the web service in the client application, you need to add a Web reference to the service. In the Solution Explorer, right-click on the Reference option and choose Add Web Reference, as shown in Figure 8-15.

Figure-8.15.jpg

Figure 8-15. Adding a web reference

This brings up the Add Web Reference page, as shown in Figure 8-16.

image.png
Figure 8-16. The Add Web Reference locator

As you can see from Figure 8-16, you can choose from any existing web service. Those web services can exist on your localhost drive in the Microsoft Universal Description, Discovery, and Integration Business Registry (UDDI) or somewhere on the Internet. You just need to provide the URL if the service in the Address text box. In this case, you need to provide the URL of OrderRetrievalService's .asmx page and then you need to add "?wsdl" at the end of URL. You can specify the .aspx file and URL path where the service entry point is located in the Address field of the wizard. As you can see in figure 8-17, I passed the URL of the service with the MCB server. You need to replace MCB with localhost of you're using your local development machine as the default web server. Figure 8-17 shows the contents of the service.

Figure-8.17.jpg

Figure 8-17. Web services available on the local server

Now click the Add reference button, which adds a reference of the service to the project. As you can see in Figure 8-18, the web reference "mcb" is added to the project. To access Web services on this sever, you need to use this namespace. You can even rename the namespace by right-clicking and choosing Rename.

Figure-8.18.jpg

Figure 8-18. Viewing OrderRetrievalService with the Add Reference button

Clicking the Add Reference button causes VS.NET to generate three files for referencing the service:

  • A discovery file (Service1.disco)
  • A WSDL file (Service1.wsdl)
  • A proxy file (Service1.cs)

As already discussed, the discovery file is an XML file helps the client to find the service. The WSDL file describes the methods and classes supplied by the service in XML and define the format that messages need to adhere to when exchanging information with the client. The proxy file, although not displayed in the Solution explorer, is a C# stub file generated so that the programmer can call the methods in the web service with the same exact method names and method parameter structure as the web service contains. You may know about proxies used with the Interface Definition Language (IDL) or Java Remote Method Invocation (RMI).

These proxies wrap the method calls to the Web service so that the convoluted exchange of parameter passing and method invocation to the remote service are transparent to the programmer. If you expand the Web References node.

Now, everything is all set, and you're ready to call the web service's method. You can access a web service by either adding the mcb namespace (or localhost if you're using localhost as your default web server) to the project or referencing the namespace directly in the project. In our sample, we're going to call the GetOrderFromDatabase method from the Fill Order button click. Now double-click on the button to write a click event handler for the button. As you probably noticed, the class in this example is Service1. So, that's the class of which you'll be creating an instance. You're going to use Service1 through the mcb namespace, which represents the Web server.

Note: You need to add the localhost namespace if you're using localhost as your default Web server.

Once you have an instance to the service, you can call GetOrderFromDatabase in the service and assign it to a DataSet reference. To display the DataSet in a DataGrid, simply assign the DataSet to the DataGrid's DataSource, and call DataBind on the grid. Listing 8-4 shows the code for Fill order button-click handler.

Listing 8-4 Button-click event handler that calls the order retrieval web service

        private void Button1_Click(object sender, System.EventArgs e)
        {

            // construct and call the web service with the order id in the textbox
            mcb.Service1 myWebService = new mcb.Service1();
            DataSet ds = myWebService.GetOrderFromDatabase
            (Convert.ToInt32(this.TextBox1.Text));

            // bind the data to the grid
            DataGrid1.DataSource = ds;
            DataGrid1.DataBind();
        }

As you can see from Listing 8-4, I called GetOredrFromDatabase through mcb.Service1, which returns a DataSet. You use this database and bind to the data grid control and call DataGrid's DataBind method.

Compile and run the Web Application. Enter an order ID of 10248 and click the Fill Order button. Figure 8-20 shows the resulting output in Internet Explorer.

Figure-8.20.jpg
Figure 8-20. Result of the Fill method in a DataGrid

There's not much to it, huh? Actually, VS.NET takes away a lot of the WSDL, proxy, and discovery file coding so that you can concentrate on what you do best: coding and service. The truth is that without knowing anything about SOAP, XML, or even HTTP, you were able to develop this web service. In the next section you'll create a web service for entering an order into the database using this knowledge and what you know about ADO.NET.

Conclusion

Hope this article would have helped you in understanding creating the Web Service Consumer. See other articles on the website also for further reference.

adobook.jpg
This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET.