HowTo: Creating an XML Web Service Using Visual C# 

Table Of Contents
1. About this Document
2. Creating an XML Web Service Project
3. Generating Web Service Method Stubs from WSDL File
4. Implementing Web Service Methods
5. Testing the Web Service

1. About this Document

The following document describes the process for creating an SOAP/XMLP web service with Visual C#. During the course of this walkthrough, you will accomplish the following activities:

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

  • Run the Web service in debug mode and test using the generated client application.

To complete the walkthrough, you must provide the following:

  • You must provide a machine that meets the requirements for creating a web service project.

For this guide, you will create a simple web service application that implements the methods described in the following web service description (WSDL): http://openimmunizationsoftware.us:6394/ImmMessagingServer/client_Service?wsdl

To create an ASP.NET Web Service application

  1. On the File menu, choose New Project.

  2. In the New Project dialog box, select the ASP.NET Web Service Application icon (you may need to ensure that .NET framwork 3.5 is selected in the drop down menu at the top of the dialog box) .

  3. Name the project IIS.

  4. Click OK to create the project.

  5. In Code Window, change the value of the WebService(Namespace="..")] attribute to reflect the desired domain name for your web service.  For example:

        
    WebService(Namespace="http://nyc.gov")]

  6. In Solution Explorer, rename the service from Service1 to  ImmunizationInformationSystemService.

  7. Inspect the code that has been generated in the Code Window. You should have a class that contains a public WebMethod called HelloWorld.

    Web Service Default

You will now generate stubs for the web service methods using the WSDL file and Visual Studio's wsdl command line tool. We will use the WSDL provided by the Transport Layer Expert Panel (TLEP): 
http://openimmunizationsoftware.us:6394/ImmMessagingServer/client_Service?wsdl

To generate web service method stubs from a WSDL file

  1. In your start menu, navigate to the Visual Studio folder, expand the  Visual Studio Tools folder and select the  Visual Studio .Net command prompt.

    Visual Studio Command Prompt

  2. Use the wsdl command to generate the web service method stubs:
 wsdl http://openimmunizationsoftware.us:6394/ImmMessagingServer/client_Service?wsdl /language:CS /server
- /language: specifies the language to use for the generated proxy/stub class. Choose from CS, VB, JS, VJS. Default is CS (C#).                                  
- /server: generates an abstract class for an XML web service implementation. Default is to generate client proxy classes.

You should now have a file name client_Service.cs that contans the web service method stubs implemented in C#. If you open client_Service.cs you should see the following code

 GeneratedWebServiceMethodStubs

In the previous section you generated web service method stubs from a wsdl file. The method stubs are defined in an abstract class called client_Service, and the methods themselves are abstract. You will now add this client_Service to your web service project and implement the web service methods.

To Implement the Web Service Methods

  1.  Add the client_Service class to your project by dragging the client_Service.cs file into the Solution Explorer on the right side of the Visual Studio window.

  2.  Edit the ImmunizationInformationSystemService so that it implements the client_Service class. This is done by appending the inheritance operator ( : ) after the class name followed by the name of the class we wish to inherit. For example:

    public class ImmunizationInformationSystemService : client_Service {}

  3. Implement the connectivityTest() and sumitSingleMessage() methods inside of the ImmunizationInformationSystemService class.

The result should look like the contents of the image below:

Implement Web Service Methods

5. Testing the Web Service

We can test the web service by running the application in debug mode by navigating to the Debug menu and selecting Start Debugging. When we do this a internet browser should open to a page that displays the name of our web service and the list of available methods:

Debugging the web service

Clicking on the Service Description link will bring us to the service description, which is an XML document that uses the Web Services Description Language (WSDL).  The service description describes what services are available and how to interact with those services. Web Serivice clients will use this WSDL to determine how to communicate with our web service.

Clicking on one of the methods listed on this page will bring us to a generic client application that we can use to test our web service by invoking the specified method. Let try this now by clicking on the connectivityTest method:

Connectivity Test

This page displays the portions of the wsdl that are relevant to the connectivityTest method. We can invoke the method by filling in a value for the echoBack textbox and clicking the Invloke button.