Web Services Execution Model
Client Application
|
V
SOAP Request via HTTP
|
V
IIS Web Server
|
V
ASP.NET Runtime
|
V
[.asmx File] => [WebService Class] => [WebMethod() Functions]
|
V
SOAP Response via HTTP
|
V
Client Application
This flow illustrates how a client application interacts with a web service through a structured pipeline, from initiating a request to receiving a response via HTTP, all handled through IIS and ASP.NET runtime.
Code Implementation
Problem : MathService
MathService.asmx.vb
<script runat="server">
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
<WebService(Namespace:="http://tempuri.org/")>
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
Public Class MathService
Inherits System.Web.Services.WebService
<WebMethod(Description:="Add two numbers")>
Public Function Add(ByVal a As Double, ByVal b As Double) As String
Return (a + b).ToString()
End Function
<WebMethod(Description:="Subtract two numbers")>
Public Function Subtract(ByVal a As Double, ByVal b As Double) As String
Return (a - b).ToString()
End Function
<WebMethod(Description:="Multiply two numbers")>
Public Function Multiply(ByVal a As Double, ByVal b As Double) As String
Return (a * b).ToString()
End Function
<WebMethod(Description:="Divide two numbers")>
Public Function Divide(ByVal a As Double, ByVal b As Double) As String
If b = 0 Then
Return "Division by zero not allowed."
Else
Return (a / b).ToString()
End If
End Function
End Class
</script>
Default.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html>
<html>
<head>
<title>Math Client</title>
</head>
<body>
<form id="form1" runat="server">
Enter A: <asp:TextBox ID="txtA" runat="server" /><br />
Enter B: <asp:TextBox ID="txtB" runat="server" /><br />
Operation:
<asp:DropDownList ID="ddlOperation" runat="server">
<asp:ListItem>Add</asp:ListItem>
<asp:ListItem>Subtract</asp:ListItem>
<asp:ListItem>Multiply</asp:ListItem>
<asp:ListItem>Divide</asp:ListItem>
</asp:DropDownList><br />
<asp:Button ID="btnCalculate" runat="server" Text="Calculate" OnClick="btnCalculate_Click" /><br />
<asp:Label ID="lblResult" runat="server" Font-Bold="True" />
</form>
</body>
</html>
Default.aspx.vb
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnCalculate_Click(sender As Object, e As EventArgs)
Dim proxy As New MathLibReference.MathService()
Dim a As Double = Convert.ToDouble(txtA.Text)
Dim b As Double = Convert.ToDouble(txtB.Text)
Dim result As String = ""
Select Case ddlOperation.SelectedValue
Case "Add"
result = proxy.Add(a, b)
Case "Subtract"
result = proxy.Subtract(a, b)
Case "Multiply"
result = proxy.Multiply(a, b)
Case "Divide"
result = proxy.Divide(a, b)
End Select
lblResult.Text = "Result: " & result
End Sub
End Class