Create and consume web service Step by Step


Before start

Set the virtual directory of your web service. It must be:

Create a web service

Create a web services class file, suffixed with .asmx (example: MathService.asmx)

<%@ WebService Language="C#" Class="MathService" %>

using System;
using System.Web.Services;

public class MathService : WebService {

   [WebMethod]
   public float Add(float a, float b)
   {
       return a + b;
   }

   [WebMethod]
   public float Subtract(float a, float b)
   {
       return a - b;
   }

   [WebMethod]
   public float Multiply(float a, float b)
   {
       return a * b;
   }

   [WebMethod]
   public float Divide(float a, float b)
   {
       if (b==0) return -1;
       return a / b;
   }

}

This web service contains 4 public method, the client can use them in their client program

Consuming a web service

To consume the web service, you need use the Web Services Description Language file.
Create a WSDL file using IE browser:

Save the result of http://localhost/mathservice.asmx?wsdl to MathService.wsdl

Use command-line tool (WSDL.exe) included in the SDK to create a proxy class that is similar to the class defined in the .asmx file. (It will contain only the WebMethod methods.)

wsdl.exe /l:CS /n:MathService /out:MathService.cs MathService.wsdl

note: when you create wsdl file, it will contain a default proxy url in WSDL file.
This URL may cause problem. For example, we create the wsdl in local computer using
http://localhost/MathService.asmx, but the real URL may not be localhost. We should
point a url for wsdl.exe command like:
wsdl.exe /l:CS /n:MathService /out:MathService.cs /proxy:myURL MathService.wsdl
Use CSC or VBC to Create web service proxy DLL : 
csc.exe /t:library /r:System.Web.dll,System.Web.Services.dll /out:bin\MathService.dll MathService.cs
 
Create a ClientApplication to consume this web service: 
The client application is suffixed by .aspx,For example, MathServiceClient.aspx
 
<%@ Import Namespace="MathServiceVB" %>

<html>

<script language="VB" runat="server">

  Dim Op1 As Single = 0
  Dim Op2 As Single = 0

  Public Sub Submit_Click(Sender As Object, E As EventArgs)

      Try

        Op1 = Single.Parse(Operand1.Text)
        Op2 = Single.Parse(Operand2.Text)

      Catch Exp As Exception
          ' Ignored
      End Try

      Dim Service As MathServiceVB.MathService = New MathServiceVB.MathService()

      Select (CType(sender,Control).ID)

        Case "Add" :
            Result.Text = "<b>Result</b> = " & Service.Add(Op1, Op2).ToString()
        Case "Subtract" :
            Result.Text = "<b>Result</b> = " & Service.Subtract(Op1, Op2).ToString()
        Case "Multiply" :
            Result.Text = "<b>Result</b> = " & Service.Multiply(Op1, Op2).ToString()
        Case "Divide" :
            Result.Text = "<b>Result</b> = " & Service.Divide(Op1, Op2).ToString()
      End Select
  End Sub

</script>

<body style="font: 10pt verdana">

  <h4>Using a Simple Math Service </h4>

  <form runat="server">

  <div style="padding:15,15,15,15;background-color:beige;width:300;border-color:black;border-width:1;border-style:solid">

    Operand 1: <br><asp:TextBox id="Operand1" Text="15" runat="server"/><br>
    Operand 2: <br><asp:TextBox id="Operand2" Text="5" runat="server"/><p>

    <input type="submit" id="Add" value="Add" OnServerClick="Submit_Click" runat="server">
    <input type="submit" id="Subtract" value="Subtract" OnServerClick="Submit_Click" runat="server">
    <input type="submit" id="Multiply" value="Multiply" OnServerClick="Submit_Click" runat="server">
    <input type="submit" id="Divide" value="Divide" OnServerClick="Submit_Click" runat="server">

    <p>

    <asp:Label id="Result" runat="server"/>

  </div>

  </form>

</body>
</html>
Running MathServiceClient.aspx in IE