Export (0) Print
Expand All
9 out of 28 rated this helpful - Rate this topic

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

Visual Studio .NET 2003

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 Creating Web Projects and Visual Studio .NET Software Requirements.

Creating the XML Web Service Project

Visual Studio provides an ASP.NET Web Service project template to help you create XML Web services in Visual Basic and Visual C#. For more information, see ASP.NET Web Service Projects in Visual Studio.

To create an ASP.NET Web Service Project

  1. On the File menu, point to New, and then click Project.
  2. In the New Project dialog box, select either the Visual Basic Projects or the Visual C# Projects folder.
  3. Click the ASP.NET Web Service icon.
  4. 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 machine, "http://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 XML Web services are located on a Web server and derive their name from the virtual directory specified on that server.
    Note   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.
  5. 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. For more information, see ASP.NET Web Service Projects in Visual Studio.

Implementing the XML Web Service

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 Creating an XML Web Service Method.

To add an XML Web service method

  1. Double-click the Component Designer's design surface to view the code-behind file.
    Note   Solution Explorer hides code-behind files by default. Click Show All Files in the Solution Explorer toolbar to see hidden files. Alternatively, on the Project menu, click Show All Files.
    Tip   If you choose to rename the class, be sure to change the class name in the Class attribute of the WebService directive.
  2. In the code-behind file, locate the code for the Service1 class declaration. Replace the System.Web.Services.WebService attribute code with the following code (shown in bold) before the class declaration:
     ' Visual Basic
    <System.Web.Services.WebService( _
       Namespace:="http://Walkthrough/XmlWebServices/", _
       Description:="A temperature conversion service.")> _
    Public Class Service1
    
    // C#
    [System.Web.Services.WebService(
       Namespace="http://Walkthrough/XmlWebServices/",
       Description="A temperature conversion service.")]
    public class Service1 : 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 Using the WebService Attribute.

  3. In the Service1 class, add the following code to declare the ConvertTemperature function:
     ' Visual Basic
    <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
    
    // C#
    [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 Using the WebMethod Attribute.

  4. Right-click Service1.asmx in Solution Explorer and then click Set as Start Page on the shortcut menu.
  5. Save the solution.

Debugging the XML Web Service

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 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 Debugging XML Web Services in Managed Code.

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

To use a breakpoint and start the XML Web service with debugging

  1. On the Debug menu, click New Breakpoint.

    On the Function tab, type ConvertTemperature in the Function box and click OK to add a breakpoint on the ConvertTemperature method declaration. For more information, see New Breakpoint Dialog Box.

  2. On the Debug menu, click Start.

    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.

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

    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.

  4. On the Service helper page, click the ConvertTemperature link.
  5. 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 and while halted, you can perform a variety of tasks. For more information, see Using the Debugger and Debugging Tools for Inspecting Your Program.

  6. On the Debug menu, click Continue to continue processing.
  7. 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.

  8. On the Debug menu, click Clear All Breakpoints.

Deploying the XML Web Service

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 Deploying 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 Project, and then click New Project.
  2. Select the Setup and Deployment Projects folder, 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, Primary output and Debug Symbols. For more information, see Adding and Removing Project Outputs in the File System Editor.
    • The Content Files group consists of the following files for the XML Web service: Service1.asmx, Global.asax, and Web.config. For more information, see Deploying XML Web Services in Managed Code.
    • The Primary output group consists of the project DLL, TempConvert1.dll, and its dependencies.
    • The Debug Symbols group consists of the project PDB file, TempConvert1.pdb.
  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 Project.
  3. In the Destination project folder box, enter the location to which you want to copy the project.
  4. Click either FrontPage or File share to select a Web access method. For more information, see Changing Web Project Access Methods.
  5. Click Only files needed to run this application.
  6. Click OK.

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

See Also

Creating and Accessing XML Web Services Walkthroughs | Visual Studio Walkthroughs | Programming the Web with XML Web Services | Web Application Security at Design Time in Visual Studio | Creating XML Web Services in Managed Code

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.