Consuming a Web Service


A web service can be consumed by any client capable of calling the service and managing its results. Visual Studio makes this task easy. It allows you to set a web reference to a web service. This process is similar to setting a reference to another .NET library or COM component. After you define this web reference, Visual Studio generates a proxy class for consuming the web service. This allows you to program against the proxy class and not worry about writing web servicespecific code. Web references can be set inside web applications, windows applications, even console applications. Let's look at this process.

Defining a Web Reference

You define a web reference for your project by selecting the Add Web Reference option from the context menu for a given project. This will launch the Add Web Reference dialog box. You use this dialog box to browse to a web service and add that service as a reference.

Figure 16.7 shows the first screen of this dialog box. From here, you can search for a web service. You have a couple of options to find a service. You can navigate directly to it through the URL field. You can also browse services defined in the current solution, on the local machine, or those exposed through a local UDDI server.

Figure 16.7. The Add Web Reference dialog box.


For the example, you will bind a Windows form application called CustomerAdministration to the CustomerProfile web service. Both the Windows form application and the service happen to be in the same solution. Therefore, you can select the link Web services in this solution to bring up as list of services in the solution. This list is shown in Figure 16.8.

Figure 16.8. Web reference in the solution.


In this example, the solution list contains a couple of web services. You can use the browser window (middle left) to navigate to these services, test them, and select the one you want to add as a reference. Of course, you are looking for the GetCustomerProfile service. Selecting this service brings up the service shown in Figure 16.9.

Figure 16.9. Set the web reference name.


A web service is now selected; it has an associated URL. You are also on the same web page you saw back in Figure 16.4. You can use this window to view details about the service and even test its invocation.

When you are comfortable with the service, you need only define the reference name and add it to your project. You set the reference name using the Web Reference Name field on the right side of the dialog box. Typically, you set this to the name of the web service. In this case, use CustomerProfile. Next, click the Add Reference button to add the web reference to the project.

Viewing the Web Reference

In Figure 16.10, the proxy class is open in the editor. This proxy class is a C# class that represents the web service. The intent of the class is to encapsulate and abstract the intricacies of calling this service. This allows developers to work with the service the same way they might work with any other .NET component or class. The contents of the proxy class include methods that mimic the web methods and objects that are used for the parameters (such as the Customer object in the example).

Figure 16.10. The web reference.


Tip

If you update or modify your web service, you will need to update the web reference. To do so, you right-click the web reference and choose Update Web Reference. This will instruct Visual Studio to refresh the reference and regenerate files as necessary.


Calling the Web Service

You call the web service the same way you would call any other .NET class. This capability is courtesy of the proxy class that was generated for you. For example, to get a Customer instance, you need only to create an instance of the CustomerProfile proxy and then call its GetCustomerProfile method. Figure 16.11 shows a working example.

Figure 16.11. The customer profile service in action.


The code for the Find click event simply translates the customer ID text box into an integer and passes that value to a private routine that loads the form. This private routine creates an instance of the proxy class and then calls the appropriate method. Listing 16.5 shows this private routine for your review.

Listing 16.5. Calling the GetCustomerProfile Web Method

private void LoadCustomer(int custId) {   CustomerProfile.CustomerProfile custProfileServ =     new CustomerProfile.CustomerProfile();   CustomerProfile.Customer cust = custProfileServ.GetCustomerProfile(custId);   //bind customer information to form   textBoxName.Text = cust.Name;   textBoxEmail.Text = cust.Email;   textBoxAddress1.Text = cust.Address1;   textBoxAddress2.Text = cust.Address2;   textBoxCity.Text = cust.City;   textBoxZip.Text = cust.Zip;   textBoxPhone.Text = cust.Phone;   checkBoxEmail.Checked = cust.ContactViaEmail;   checkBoxPhone.Checked = cust.ContactViaPhone;   comboBoxState.SelectedText = cust.State; }

All web services in .NET are called this same way. A proxy is generated, and you then work with the proxy to call the service. This is true of web applications that call web services, console applications, and so on.

Note

To debug your web service from a client application, you must attach to the ASP worker process responsible for hosting the service. Refer to Chapter 9, "Debugging with Visual Studio 2005," for details on doing just that.





Microsoft Visual Studio 2005 Unleashed
Microsoft Visual Studio 2005 Unleashed
ISBN: 0672328194
EAN: 2147483647
Year: 2006
Pages: 195

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