14.2. Consuming a Web Service

Problem

You need to use a web service created by another group in your company to access data your application requires.

Solution

Add a web reference to an existing ASP.NET project using Visual Studio 2005. Create an instance of the web service class in your application and call its methods.

To add a web reference to an ASP.NET project in Visual Studio 2005:

  1. Select the project in the Solution Explorer, right-click, and select Add Web Reference from the context menu.

  2. In the Add Web Reference dialog box, enter the URL of the web service you want to consume, as shown in Figure 14-3, set the name for the web reference, and click the Add Reference button.

Visual Studio 2005 will create all the files needed to consume the web service and will place them in the App_WebReferences folder. The files include a .disco, a .discomap, and a .wsdl file for the web service.

After adding the web reference, create an instance of the web service class and call its methods in the code-behind class for the page. Examples 14-8, 14-9 through 14-10 show the .aspx file as well as VB and C# code-behind files for an example we’ve written to create an instance of the web service class from Recipe 14.1 and call its methods.

Adding a web reference

Figure 14-3. Adding a web reference

Discussion

Visual Studio 2005 makes consumption of a web service easy by creating all of the plumbing for you. You don’t have to worry ...

Get ASP.NET 2.0 Cookbook, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.