Consuming Web Services with C


Consuming Web Services with C#

Earlier in this chapter, you got a preview of how to build a simple Web service that exposed the Customers table from the Northwind database of SQL Server. This Web service was written in C# and, once it is in place, any consumer (regardless of the consumer's underlying platform) can consume this Web service.

So where can you consume Web services when using C#? Remember that the Web services you come across can be consumed in Windows Forms applications, mobile applications, databases, and more. You can even consume Web services with other Web services so you can have a single Web service made up of what is basically an aggregate of other Web services. Anything that is connected to HTTP and can understand XML in some fashion can utilize the Customers Web service that was created earlier in this chapter.

Next, you see how to consume the Customers Web service in a C#-based ASP.NET application.

Consuming Web Services Using ASP.NET

The first step in the process of consuming a Web service using ASP.NET is to create an ASP.NET project using Visual Studio 2005. This process creates a single page called Default.aspx in the project. The Solution Explorer for this project is shown in Figure 19-10.

image from book
Figure 19-10

Adding a Web Reference to Your ASP.NET Project

Now that the project is in place, right-click the project name and select Add Web Reference from the provided menu. You also see an Add Reference option in the menu, but this is for referencing objects that reside on the same server. Because you are hoping to make a reference to a remote object, you next use the Add Web Reference option. This selection from the menu pulls up the Add Web Reference dialog box.

From the Add Web Reference dialog, you can search for Web services that are located within the same project, the same server, or which are contained within UDDI. For this solution, be sure to select the option that enables you to find Web services that reside on the same server.

Note 

You might not find the earlier Customers Web service on the local machine depending on how that service was built. By default, Visual Studio 2005 uses a built-in Web server to launch, run, and test the Web services. If this default Web server is not running, then your new ASP.NET project will not find the Web service anywhere on the server. To get around this, either open another instance of Visual Studio 2005 and run the Customers Web service project or build the Customers Web service within Internet Information Services (IIS).

To find the Customers Web service, you really have to type only the URL endpoint of the service in the address bar of the dialog. Visual Studio figures out where the location of the dynamically created WSDL document on its own when just referencing Customers.asmx. However, if you are consuming a Web service from another platform, you must type the location of the WSDL document in the address bar instead.

After the reference has been made, you then should rename the reference. The reference has the default name of localhost (if the Web service resides on the same server as the consuming application) or it will have something a bit cryptic such as com.wrox.www if located elsewhere on the Internet. For this example, I renamed the reference Wrox as shown in Figure 19-11.

image from book
Figure 19-11

After you have the Add Web Reference dialog the way you want it, click the Add Reference button in the dialog to have Visual Studio generate everything you need to consume the Web service.

So, after making this reference, what is created for you? If you look at the Solution Explorer of Visual Studio shown in Figure 19-12, you see the additions.

image from book
Figure 19-12

From Figure 19-12, you can see that the WSDL document was pulled down to the application as well as some additional DISCO (discovery) documents. In addition to the WSDL document, you also find that a change was made to the ASP.NET application's web.config file.

      <appSettings>         <add key="Wrox.Customers"          value="http://localhost:1364/Wrox1/Customers.asmx"/>      </appSettings> 

This <appSettings> value gives you an object reference to the Customers Web service that can now be utilized in your code.

Building the Consuming Web Page

Now turn your attention to the Default.aspx page that is in the ASP.NET solution. Now that a reference is in place in the project, the next step is to build the means on the ASP.NET page to utilize this object in some fashion.

On the design part of the Default.aspx page, place a Button and a GridView control so that your page looks something like the one shown in Figure 19-13.

image from book
Figure 19-13

The idea is that, when the end user clicks the button contained on the form, the application sends a SOAP request to the Customers Web service and gets back a SOAP response containing the Customers table from the Northwind database, which is then bound to the GridView control on the page. Listing 19-9 shows the code for this simple application.

Listing 19-9: Consuming the Customers Web service in an ASP.NET page

image from book
      using System;      using System.Data;      using System.Configuration;      using System.Collections;      using System.Web;      using System.Web.Security;      using System.Web.UI;      using System.Web.UI.WebControls;      using System.Web.UI.WebControls.WebParts;      using System.Web.UI.HtmlControls;      public partial class _Default : System.Web.UI.Page      {          protected void Button1_Click(object sender, EventArgs e)          {              Wrox.Customers ws = new Wrox.Customers();              GridView1.DataSource = ws.GetCustomers();              GridView1.DataBind();          }      } 
image from book

The code from Listing 19-9 is the code from the code-behind page, Default.aspx.cs. This code-behind file contains only a single event-the Button1_Click event. This event occurs when the end user clicks the single button on the ASP.NET page. When this happens, first the Web service reference is instantiated as ws:

      Wrox.Customers ws = new Wrox.Customers(); 

Then the result from the ws object's GetCustomers() method is assigned to the DataSource property of the GridView control before being data bound to the control. These three simple lines of code give you the results illustrated in Figure 19-14.

image from book
Figure 19-14

In the end, a dataset is exposed from a remote server and then, using standards such as SOAP, you are able to consume this dataset over HTTP and use it inside your application. It was simple to achieve this remote procedure call because it is based upon standards.

Consuming Web Services Using Windows Forms

To show you the power of consuming Web services, you can look at another example of consumption. Remember, after the data is exposed as a Web service and becomes consumable by someone, you really can't control how that data is utilized. For instance, although you just saw the consumption of the Customers table into an ASP.NET application (which is a browser-based application), the consumer can also take this SOAP response and use it in a thick-client application (Windows Forms, for instance), a console application, a Windows service, and even another Web service.

Next, to show another example of consumption, you can work through an example of consuming the GetCustomers() WebMethod in a Windows Forms application. To accomplish this, open up Visual Studio 2005 and create a new Windows Forms project in C#. This gives you a project like the one shown in Figure 19-15.

image from book
Figure 19-15

The creation of the Windows Forms project gives you a single form (Form1.cs) that you can work with. Like the ASP.NET Web application shown earlier, to have this Windows Forms project work with the Customers Web service you created earlier, you have to make a Web Reference in the Windows Forms project. To do this, right-click the project in the Solution Explorer and select Add Web Reference from the provided menu. To add the Web reference, go through the same steps used in the ASP.NET application, naming the reference Wrox.

This does a few things to your project as shown in Figure 19-16.

image from book
Figure 19-16

Instead of making a reference in this fashion, you can also use the Data Sources tab (shown in the bottom-right-hand corner of Figure 19-16) to add a reference to the Web service. Doing it through the Data Sources dialogs also adds a proxy class to the Visual Studio toolbox that you can then easily use in your project.

For this example, I simply add a DataGridView control to the form and a little bit of style (just for alternating rows). In the code-behind of the form, the only thing to add is an instantiation of the Web service reference and then a process to bind this instantiation to the DataGridView control on the form. The code-behind for the Form1_Load event is shown in Listing 19-10.

Listing 19-10: Calling the Web service from a Windows Forms application

image from book
      using System;      using System.Collections.Generic;      using System.ComponentModel;      using System.Data;      using System.Drawing;      using System.Text;      using System.Windows.Forms;      namespace WroxWinCustomer      {          public partial class Form1 : Form          {              public Form1()              {                  InitializeComponent();              }              private void Form1_Load(object sender, EventArgs e)              {                  Wrox.Customers ws = new Wrox.Customers();                  dataGridView1.DataMember = "Customers";                  dataGridView1.DataSource = ws.GetCustomers();              }          }      } 
image from book

In this example, you use the DataGridView control's DataMember property and assign the name of the class you are consuming as well as assigning the DataSource property to be the dataset which is returned from the GetCustomers() method call. The result of this operation is shown in Figure 19-17.

image from book
Figure 19-17

I hope that after you see how this simple Web service was consumed in a Web application as well as in a thick-client application, you recognize how powerful the Web services model is and how usable it is in almost any environment. That's the power of Web services and that is why everyone is so excited about them.

I hope that after you see how this simple Web service was consumed in a Web application as well as in a thick-client application, you recognize how powerful the Web services model is and how usable it is in almost any environment. That's the power of Web services and that is why everyone is so excited about them.




Professional XML
Professional XML (Programmer to Programmer)
ISBN: 0471777773
EAN: 2147483647
Year: 2004
Pages: 215

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net