O'Reilly logo

ASP.NET Cookbook by Geoffrey T. LeBlond, Michael A Kittel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required