This course - Write a web service in C# on your local machine (Tutorial 1)

v1.0 - last updated: 08/07/2006

In this tutorial, you create an XML web service on your local disk using Visual Studio 2005's local web server.  There are 8 steps...
 


  1. Create a new web service project.
    In Visual Studio, go to the  File > New > Web Site menu.  This will give you a popup:

    In the New Web Site window, select ASP.NET Web Service. Select location as File System, and browse to a folder where you'd like to put the code (this can be a new folder).  Select Visual C# as the language, and click OK.  This will create the default code for an XML web service in the folder you chose.


  2.  
  3. Customize the code in file Service.cs.
    The default code is shown here:

    Modify the above code:
    • Rename the class from Service to Hello. Change both the class and the constructor name.
    • Change the namespace from "http://tempuri.org/" to "http://wren.cis.upenn.edu/" (or whatever web server it will eventually reside on)
    • Change the [WebMethod] attribute to [WebMethod(Description = "Returns string: Hello World.")]

  4.  
  5. Customize the code in file Service.asmx.
    The default Service.asmx file contains:
    <%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="Service" %>
    Change the class from Service to Hello, as follows:
    <%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="Hello" %>

  6. Add a Web.config file to the project.
    With the project name selected, right-click the project and then select Add New Item:


    In the Add New Item popup window, select Web Configuration File and click on Add:

  7.  
  8. Add some lines near the bottom of the Web.config file.
    Open the  Web.config file; it contains the following default information:

     
    Scroll to the bottom of the Web.config file and add some lines.  The lines to be added can be copied from here and pasted into the file:

    Save and close the Web.config file.  Adding these lines will allow you to test your web service (using an HTTP POST message) without having to write any client code.

  9.  
  10. Build the project.
     
  11. Test the project by opening the service in a browser.
    Select the Service.asmx file and right-click over it:

    NOTE: While located on your local disk drive, this service requires Internet Explorer.  If IE is not your default browser, you will first have to select Browse With...IE and make it the default for testing in Visual Studio. After that, you'll be able to select View in Browser and you'll get IE each time.  This will not alter the default browser on your machine as a whole, only for Visual Studio.

    The browser should show the HTML version of the WSDL page:


  12. NOTE: The VS.NET local disk web server is using a random port number (1130 in the above example) so it won't interfere with any other web server running on your local machine.
     
  13. Use HTTP POST to call the HelloWorld() method in the service.
    In the above page, click on HelloWorld to get its details:

    Click on the Invoke button to call the method.  After a short pause, you should see the resulting XML file that was returned:


    This means your web service is working correctly while located on your local disk drive.


 

This is the end of Tutorial 1.  See other tutorials.



Visitors: Hit Counter