11.1. Creating a Web Service

Problem

You want to create a web service.

Solution

Use Visual Studio 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 .NET File menu, choose New Project....

  2. When the New Project dialog box is displayed, go to the left pane and select either Visual Basic Projects or Visual C# Projects.

  3. Go to the right pane and click on the ASP.NET Web Service icon.

  4. Enter the name of the web service in the Location text box and click OK.

Visual Studio .NET creates a virtual directory in IIS and a project that contains a .asmx file and code-behind files for the web service, like those shown in Example 11-2 through Example 11-5. The web service is fully functional at this point but is just a shell with no useful functionality.

You need to add methods to the code-behind to expose the functionality required for your web service. The code-behind shown in Example 11-6 (VB) and Example 11-7 (C#) shows a method we have added to our example web service to return a list of books from a database. The code generated by Visual Studio .NET to support the component designer has been omitted from Example 11-6 and Example 11-7 for clarity.

Tip

You can add a web service to an existing web application by selecting the project in the Solution Explorer, and then selecting Add New Item from the File menu. When the Add New Item dialog box is displayed, select Web Service, enter the desired name ...

Get ASP.NET Cookbook 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.