14.1. Creating a Web Service

Problem

You want to create your own web service.

Solution

Use Visual Studio 2005 to create a new web service and then add methods to expose the functionality required for your web service.

To create the new web service:

  1. On the Visual Studio 2005 File menu, choose New → Web Site….

  2. When the New Web Site dialog box is displayed, select the ASP.NET Web Site template, the desired language, and the location for the web site.

  3. Right-click on the new web site in the Solution Explorer, select Add New Item, and select the Web Service template.

  4. Enter the name of the web service in the Name text box, select the desired language, and click Add.

Visual Studio 2005 creates a folder for the web site that contains an .asmx file and code-behind files for the web service, like those shown in Examples 14-2, 14-3, 14-4 through 14-5. The web service is fully functional at this point but is a shell with no useful functionality.

Tip

Visual Studio 2005 places the code-behind files for web services in the App_Code directory instead of placing them in the same folder as the .asmx file, as Visual Studio 2003 did.

You need to add methods to the code-behind to expose the functionality required for your web service. The code-behind shown in Examples 14-6 (VB) and 14-7 (C#) shows a method we have added to our example web service to return a list of books from a database.

Tip

You can add a web service to an existing web application by selecting the web site in the Solution Explorer and then selecting ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.