Web Services: Part 2 (Creating a WebService)

Creating a web service is very easy. Here I will explain creation of a Web Service and their consumption in a client application. Just have a look at the procedure.

Step 1 :
Open Visual Studio 2008 then "File" -> "New" -> "Website".

WebService1.gif

Step 2 : Set a location and give a name to your Web Service. In my case I have named it ArithmeticOperation, and press "Ok".

WebService2.gif

Now explore using the Solution Explorer and see the structure.

WebService3.gif

In the above figure it is clear that we will get one Service.cs page in the App_Code folder.

We will also get one Service.asmx file.

Let me make it clear that ASP.NET supports Web Services using a .asmx file.

A .asmx file is a text file just like your .aspx page.

A .asmx file starts with an ASP.NET directive Web Service, and sets the language to C#, Visual Basic, or Jscript.

Step 3 : Now go to your Service.cs page.

WebService4.gif

In this page you will see one method helloworld() returning a string.

WebService5.gif

Step 4 : Now just debug your service by pressing F5. Then you will be shown a window like this:

WebService6.gif

Now your service is ready that has only one method i.e, HelloWorld().

To call this method just click on HelloWorld.

Then click on invoke.

WebService7.gif

Now, see the output that is an XML formatted output.

WebService8.gif

Let us add some more methods.

Go to your Service.cs page and add the following methods:

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    } 
    [WebMethod]
    public int sum(int a, int b)
    {
        return a + b;
    }
    [WebMethod]
    public int subtract(int a, int b)
    {
        return a - b;
    }
    [WebMethod]
    public int multiply(int a, int b)
    {
        return a * b;
    }
    [WebMethod]
    public int divide(int a, int b)
    {
        return a / b;
    }

Now build your application and see the result.

WebService9.gif

Now your service contains a total of five methods. Let us call one of them.

Click on the sum method.

WebService10.gif

Now enter two numbers, because we have defined the sum() method in our service to take two arguments of integer type.

Let us enter 15 and 12.

Click on the invoke button.

WebService11.gif

Now see the output. It is showing us 27 in XML format.

WebService12.gif

  • [WebMethod] attribute

    The "[WebMethod]" attribute is a way by which the methods of the service are enabled to be called remotely.

    This attribute is written just above the webmethod.
     
  • Description parameter

    "Description" is an extra parameter that allows us to describe the webmethod.

    Using this we simply describe our methods.

Now once again go to your Service.cs page and add the Description parameter to each and every method and whatever a description shows, that is what your webmethod is going to do.

    [WebMethod(Description="Finds the sum of two numbers")]
    public int sum(int a, int b)
    {
        return a + b;
    }
    [WebMethod(Description = "Finds the difference of two numbers")]
    public int subtract(int a, int b)
    {
        return a - b;
    }
    [WebMethod(Description = "Finds the product of two numbers")]
    public int multiply(int a, int b)
    {
        return a * b;
    }
    [WebMethod(Description = "Finds the quotient of two numbers")]
    public int divide(int a, int b)
    {
        return a / b;
    }

Now build your service by pressing the F5 key and see the result.

You will see the description of the methods just below your webmethod.

WebService13.gif


Similar Articles