A Calculator XML Web Service

Document Sample
A Calculator XML Web Service Powered By Docstoc
					               A Calculator XML Web Service
Enter the following code and save it as Calculator.asmx in your C:\inetpub\wwwroot directory to create a
Simple XML Web Service.

<%@ webService Language="VB" Class="Calculator" %>

Imports System.Web.Services

public Class Calculator : Inherits WebService
  <WebMethod()> Public Function Add(A As Decimal, _
    B As Decimal) As Decimal
    Return(A + B)
   End Function
End Class


                                                                  When you open the web page at
                                                                  http://localhost/calculator.asmx,
                                                                  you will see a page like that on the
                                                                  left. You can click on the Add
                                                                  function to test it.




                                                                  You can also open the web page at
                                                                  http://localhost/calculator.asmx
                                                                  ?WSDL to see a page like that on the
                                                                  left. This shows you the XML Schema
                                                                  for these Web Services.
You can create a Discovery Document to make it easier for others to find your web services:

<?xml version="1.0" ?>
<disco:discovery xmlns:disco="http://schemas.xmlsoap.org/disco"
    xmlns:scl="http://schemas.xmlsoap.org/disco/scl">
    <wsdl:contractRef ref="http://localhost/calculator.asmx?WSDL"/>
</disco:discovery>


To demonstrate the power of .Net Web Services, we are going to be creating a web service using Visual
Studio .Net.

First of all, create a new ASP .Net Web Service using Visual Studio .Net – you can view the
Service1.asmx file in a Web Browser, and you will see the framework for the service, but we have not
added any functionality to it yet.

Then remove the comments on the lines with Hello World and save the file (Ctrl-S), and then build and
view it in the browser. You will see that you now have a working Web Service.

Now lets re-comment the Hello World lines in the file and we are now going to add some lines to our file
to create a four function calculator


     <WebMethod()> Public Function Add(ByVal A As Decimal, _
                                   ByVal B As Decimal) As Decimal
         Return A + B
     End Function

     <WebMethod()> Public Function Subtract(ByVal A As Decimal, _
                                   ByVal B As Decimal) As Decimal
         Return A - B
     End Function

     <WebMethod()> Public Function Multiply(ByVal A As Decimal, _
                                   ByVal B As Decimal) As Decimal
         Return A * B
     End Function

     <WebMethod()> Public Function Divide(ByVal A As Decimal, _
                                   ByVal B As Decimal) As Decimal
         Return A / B
     End Function


Now add a Description to each of the methods:

<WebMethod(Description:="This is a function to subtract two
integers")>
Creating Regions

#Region "Calc Functions"

      Your Code Goes Here…

#End Region


Now create the client application in Visual Studio .Net and once you have built a form that looks like …




You then need to Add the Web Reference under Project…

Adds the System.Web.Services.dll as a Reference to the References
section of the VS .Net Interface


Then we need to add the following line in the General Declarations area of Form1 (after the Inherits)

Dim oCalc As New localhost.Service1()

Then we need to add the functionality to each button of our calculator, so it functions correctly…

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
        TextBox3.Text = oCalc.Add(CDec(TextBox1.Text), _
            CDec(TextBox2.Text)).ToString()
    End Sub

				
DOCUMENT INFO