START PREV NEXT

Hello World Web Service in Visual C#

 

This page assumes that the Visual Studio .Net 2003 version is installed with all default options.

Visual Studio makes Web service development very simple process.

 

 

Hello World :

 

We will create a Hello World Web Service that will have only one method named ‘Hello World’ .

The client of this application can call this service by using SOAP or HTTP-GET or HTTP-POST request, the service replies with a string message “Hello World’.

 

Step I:

 

  1. Start Visual Studio .Net IDE.
  2. Use File menu, then click New Project.

 

 

 

 

 

 

  1. Use C# from Project Types, then select “ASP .NET Web Service” from Templates.
  2. Use the name HelloWorld in the Location. The full path defines the location of files that IIS server uses for this service.
  3. Click OK.

 

Step II:

 

  1. After Visual Studio generates the template of the project, edit the code.  Use View-> Solution Explorer window.

Right-click ok on Service.asmx. Note the code file that is opened has the .cs extension for C#.

 

  1. Find the following section towards the end of the file and uncomment the lines

            [WebMethod]

            public string HelloWorld()

            {

                  return "Hello World";

            }

 

  1. Use ‘Build Solution’ option from build menu to build the solution.

 

 

Step III:

 

  1. Test the Web Service that we built just now. Press F5 to Start the Web Service from within the Visual Studio. This will open up the Internet Explorer with the URL used to build the Service “http://localhost/XMLWebServices/Tutorial/HelloWorld/Service1.asmx”.
  2. The page displays the description about the Web service and proper use of “Namespace”.

 

 

 

 

 

  1. Click the HellowWorld method on the top of the page. This will display the details of the Hello World method. This includes the section that shows the structure of SOAp or HTTP-GET and HTTP-POST requests to the service. The highlighted parts indicate that they are variable at runtime.

 

 

 

 

 

 

  1. Click on Invoke button. This will invoke the Web Service using HTTP-GET protocol. The response will be:

  <?xml version="1.0" encoding="utf-8" ?>

  <string xmlns="http://tempuri.org/">Hello World</string>

 

    This response is not for end-user. This is a response to the Web Service client for it’s internal use. But you can see the text “Hello World’.