A Calculator XML Web Service



A Calculator XML Web Service - CS

Enter the following code and save it as Calculator.asmx in your C:\inetpub\wwwroot directory to create a Simple SOAP Application.

using System.Web.Services;

public class Calculator : System.Web.Services.WebService

{

[WebMethod(Description="This is a function to Add two numbers")]

public float Add(float A, float B)

{

return (A + B);

}

}

When you open the web page at , 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 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:

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 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)

{ return (A / B); }

Now add a Description to each of the methods:

[WebMethod(Description="Function to add two numbers")]

Creating Regions

#region Calculator Functions

Your Code Goes Here…

#endregion

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

[pic]

You then need to Add the Web Reference under Project… (you can also right-click on References, in Solution Explorer, and select Add Web Reference).

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

Add a reference to the References section of the code of the form:

using WebApplication1.localhost;

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

Private void button1_Click(object sender, System.EventArgs e)

{

Service1 oCalc = new Service1();

textBox3.Text = oCalc.Add(float.Parse(textBox1.Text),

float.Parse(textBox2.Text)).ToString();

}

Private void button2_Click(object sender, System.EventArgs e)

{

Service1 oCalc = new Service1();

textBox3.Text = oCalc.Subtract(float.Parse(textBox1.Text),

float.Parse(textBox2.Text)).ToString();

}

Private void button3_Click(object sender, System.EventArgs e)

{

Service1 oCalc = new Service1();

textBox3.Text = oCalc.Multiply(float.Parse(textBox1.Text),

float.Parse(textBox2.Text)).ToString();

}

Private void button4_Click(object sender, System.EventArgs e)

{

Service1 oCalc = new Service1();

textBox3.Text = oCalc.Divide(float.Parse(textBox1.Text),

float.Parse(textBox2.Text)).ToString();

}

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download