Walkthrough: Creating a Web Service Using Visual Basic or Visual C#

The following walkthrough describes the process for creating a Web service that converts temperatures measured in Fahrenheit to Celsius using Visual Basic or Visual C#.

During the course of this walkthrough, you will accomplish the following activities:

  • Create a Web service using the ASP.NET Web Service project template.

  • Implement the Web service.

  • Run the Web service in debug mode.

  • Deploy the Web service.

To complete the walkthrough, you will need access to a machine that meets the requirements for creating a Web project. For more information, see Walkthrough: Creating and Using an ASP.NET Web Service in Visual Web Developer.

Creating the Web Service Project

Visual Studio provides an ASP.NET Web Service project template to help you create Web services in Visual Basic and Visual C#.

To create an ASP.NET Web Service Project

  1. On the File menu, choose New Web Site.

  2. In the New Web Site dialog box, select the ASP.NET Web Service icon.

  3. Enter the address of the Web server on which you will develop the Web service and specify TempConvert1 as the directory name, such as "https://MyServer/TempConvert1". By default, the project uses your local computer, "https://localhost".

    Note

    For some project types, the Name textbox is unavailable because specifying the location sets the project name instead. For example, Web applications and Web services are located on a Web server and derive their name from the virtual directory specified on that server.

    Note

    You develop Web services on a development server. By default, the development server is your local machine. Typically, you develop and build the project on a development server, and then you deploy it to another server (the deployment server) that will host the Web service using a deployment project. However, if you are developing directly on the server that will host the Web service, the development server and deployment server are the same.

  4. Click OK to create the project.

Visual Studio automatically creates the necessary files and includes the needed references to support a Web service. When you create a Web service project in Visual Studio, you see the Component Designer for Service1.asmx.

Implementing the Web Service

The next step is to write the code to implement the functionality of the Web service that clients will access. For Web services created in Visual Studio, a hidden code-behind file associated with the Web service's .asmx file that Visual Studio created for you contains this code. For more information, see How to: Create a Web Service Method.

To add a Web service method

  1. In the Service.vb code file, locate the code for the Service class declaration. Replace the System.Web.Services.WebService attribute code with the following code (shown in bold) before the class declaration:

    <System.Web.Services.WebService( _
       Namespace:="https://Walkthrough/XmlWebServices/", _
       Description:="A temperature conversion service.")> _
    Public Class Service
    
    [System.Web.Services.WebService(
       Namespace="https://Walkthrough/XmlWebServices/",
       Description="A temperature conversion service.")]
    public class Service : System.Web.Services.WebService
    

    Attaching the WebService attribute to a Public class makes it possible for you to include additional information about the Web service, such as a namespace for the Web service and a description of the Web service. The description property of this attribute is included in the Service help page. For more information, see How to: Use the WebService Attribute.

  2. In the Service class, add the following code to declare the ConvertTemperature function:

    <WebMethod(Description:="This method converts a temperature in " & _
           "degrees Fahrenheit to a temperature in degrees Celsius.")> _
    Public Function ConvertTemperature(ByVal dFahrenheit As Double) _
                                       As Double
        Return ((dFahrenheit - 32) * 5) / 9
    End Function
    
    [WebMethod(Description="This method converts a temperature in " +
           "degrees Fahrenheit to a temperature in degrees Celsius.")]
    public double ConvertTemperature(double dFahrenheit)
    {
       return ((dFahrenheit - 32) * 5) / 9;
    } 
    

    Attaching the WebMethod attribute to a Public method exposes that method as part of the Web service. The description property of this attribute is included in the Service help page and the Service method help page. For more information, see How to: Use the WebMethod Attribute.

  3. Right-click Service.asmx in Solution Explorer and then click Set as Start Page on the shortcut menu.

  4. Save the solution.

Debugging the Web Service

Visual Studio offers several methods to build and run a Web service from the IDE, such as:

  • Start (with Debugging)

  • Start Without Debugging

  • View in Browser

As a Visual Studio project, this Web service has separate configurations for Release and Debug versions. Because you created this project using the ASP.NET Web Service project template, Visual Studio automatically created these configurations and set the appropriate default options and other settings. For more information, see How to: Set Debug and Release Configurations.

In this walkthrough, you will place a breakpoint in the Web service and use the Start (with Debugging) method. For more information, see How to: Debug Web Services in Managed Code.

Prior to debugging, verify your debug settings. For more information, see Debugging Preparation: XML Web Service Projects.

To use a breakpoint and start the Web service with debugging

  1. On the Debug menu, choose New Breakpoint, then Break At Function.

    On the Function tab, type ConvertTemperature in the Function box and click OK to add a breakpoint on the ConvertTemperature method declaration.

  2. On the Debug menu, click Start, then click Ok in the Debugging Not Enabled window to start debugging.

    This command instructs Visual Studio to run the Web service in the debugger. Visual Studio builds the project and deploys it to the designated development server. Upon completion, the default browser displays the .asmx file from the deployment server.

    When you open an .asmx file in a browser, the Web service returns a Service helper page that provides information about the Web service. The Service Description link takes you to an XML document that contains the formal service description of the Web service. For more information, see XML Web Service Description.

  3. On the Service helper page, click the ConvertTemperature link.

  4. In the dFahrenheit box, type the number 212, and then click the Invoke button.

    When processing reaches the ConvertTemperature function, processing stops. The Visual Studio debugger highlights the line containing the breakpoint; while it is halted, you can perform a variety of tasks. For more information, see Debugger Roadmap and Viewing Data in the Debugger.

  5. On the Debug menu, click Continue to continue processing.

  6. The Web service responds by returning the converted value in an XML document resembling the following:

    <?xml version="1.0" encoding="utf-8" ?>
    <double xmlns="https://Walkthrough/XmlWebServices/">100</double>
    

    To stop running the Web service and return to the Code Editor, on the Debug menu, click Stop Debugging.

  7. On the Debug menu, click Delete All Breakpoints.

Deploying the Web Service

To make your Web service available to others, you must deploy it to a Web server that is accessible to the clients you wish to support. To deploy the Web service to a server other than the development server, you can either add a Web Setup project or copy the required files to the destination server. In this walkthrough, you can choose how to deploy this Web service. For more information, see How to: Deploy Web Services in Managed Code.

To deploy the Web service using a Web Setup project

  1. On the File menu, point to Add, and then click New Project.

  2. Select the Other node, then the Setup and Deployment Projects node, and then click Web Setup Project.

  3. In the Name box, type TempConvert1WebSetup, and then click OK.

    Note

    By default, the installer uses the deployment project name when creating the virtual directory on your deployment server.

  4. In the left pane of the File System Editor, select Web Application Folder. For more information, see File System Editor.

  5. In Solution Explorer, right-click TempConvert1WebSetup, point to Add, and then click Project Output.

  6. In the Add Project Output Group dialog box, select Content Files. For more information, see How to: Add and Remove Project Outputs in the File System Editor.

  7. Click OK.

  8. In Solution Explorer, right-click the TempConvert1WebSetup project, and then on the shortcut menu, click Build.

    This creates a Windows Installer file in the local project directory. Executing this file installs the Web application.

To deploy the Web service by copying the project

  1. In Solution Explorer, select the TempConvert1 project.

  2. On the Project menu, click Copy Web Site.

  3. Click the icon next to the Connect To dropdown box to open the Open Web Site dialog box. Browse to the location to which you want to copy the project.

  4. In the Source Web Site pane, select the files to copy and move them to the Remote Web Site pane by clicking the right-arrow icon. .

  5. Click Copy Web Site to copy the Web site.

Finally, to create a client application that accesses this Web service, see one of the following:

See Also

Concepts

Visual Studio Walkthroughs

Other Resources

Creating and Accessing Web Services Walkthroughs

Programming the Web with Web Services

Creating Web Services in Managed Code