Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Walkthrough: Creating an XML Web Service Using Visual Basic or Visual C# 

Visual Studio 2005

The following walkthrough describes the process for creating an XML 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 an XML Web service using the ASP.NET Web Service project template.

  • Implement the XML Web service.

  • Run the XML Web service in debug mode.

  • Deploy the XML 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 Guided Tour of Creating Web Sites in Visual Web Developer and Walkthrough: Creating and Using an ASP.NET Web Service in Visual Web Developer.

Visual Studio provides an ASP.NET Web Service project template to help you create XML 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 XML Web service and specify TempConvert1 as the directory name, such as "http://MyServer/TempConvert1". By default, the project uses your local computer, "http://localhost".

    NoteNote

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

    NoteNote

    You develop XML 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 XML Web service using a deployment project. However, if you are developing directly on the server that will host the XML 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 an XML Web service. When you create an XML Web service project in Visual Studio, you see the Component Designer for Service1.asmx.

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

To add an XML 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:="http://Walkthrough/XmlWebServices/", _
       Description:="A temperature conversion service.")> _
    Public Class Service
    

    [System.Web.Services.WebService(
       Namespace="http://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 XML Web service, such as a namespace for the XML Web service and a description of the XML 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 XML 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.

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

  • Start (with Debugging)

  • Start Without Debugging

  • View in Browser

As a Visual Studio project, this XML 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 XML Web service and use the Start (with Debugging) method. For more information, see How to: Debug XML 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 XML 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 XML 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 XML Web service returns a Service helper page that provides information about the XML Web service. The Service Description link takes you to an XML document that contains the formal service description of the XML 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 XML Web service responds by returning the converted value in an XML document resembling the following:

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

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

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

To make your XML 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 XML 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 XML Web service. For more information, see How to: Deploy XML Web Services in Managed Code.

To deploy the XML 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.

    NoteNote

    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 XML 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 XML Web service, see one of the following:

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft