Simple calculator web service #mesameergaikwad

 

Simple calculator web service



Web Services

The topics of this article are:

  • Overview of Web Services
  • How to create Web Service with ASP.Net
  • How to use a Web Service from windows Form client

What is Web Services?

Web Services are server-side programs that listen for messages from client applications and return specific information.

This information may come from the Web Service itself, from other components in the same domain, or from the other Web Services.

One big feature of Web Services is, Web Services are able to communicate with heterogeneous applications because Web Services present the information in XML format.

The following figure shows how Web Services are able to communicate with a heterogeneous application.

Web Service Architecture

Web Services can use the SOAP protocol, which is a standard defined by many companies. A big advantage of a Web Service is its platform independence. Web Services are also useful for developing a .NET application on both the client and server-side. The advantage here is that client and the server can emerge independently. A service description is created with the Web Service Description Language (WSDL). WSDL is the XML-based description of a Web Service.

A WSDL document contains information about the method that a Web Service supports and how they can be called.

What is UDDI?

UDDI is an XML-based standard for describing, publishing, and finding Web Services.

  • UDDI stands for Universal Description, Discovery, and Integration.
  • UDDI is a specification for a distributed registry of Web Services.
  • UDDI is a platform-independent, open framework.
  • UDDI can communicate via SOAP, CORBA, Java RMI Protocol.
  • UDDI uses WSDL to describe interfaces to Web Services.
  • UDDI is seen with SOAP and WSDL as one of the three foundation standards of Web Services.
  • UDDI is an open industry initiative enabling businesses to discover each other and define how they interact over the Internet.

Creating Web Service

To implement the Web Service you can derive the Web Service class from “System.Web.Services.WebService”.

Web Service attribute

The subclass of a web service should be marked with the web service attribute. The WebService attribute has the following properties:

  • Description: A description of the service that will be in the WSDL document.
  • Name: Gets or Sets the name of the Web Service.
  • Namespace: Gets or sets XML namespace for Web Service. The default value is http://tempuri.org, which is ok for testing, but before you make a service public you should change the namespace.

WebMethod Attribute: All the methods available from the Web Services must be marked with the WebMethod attribute.

The method that is not marked with the WebMethod attribute, they can not be called from the client-side.

Creating A Simple ASP.Net Web Service

1. Creating a Web Service Project

Create a new Web Service project by selecting “File” -> “New” -> “Project…” and choose the “ASP.Net Empty Web Application” template. Give the name to the project “SimpleCalcService”:

2. Add New Item

Right-click on the project then select “Add New Item” -> WebService then provide the name “CalcService.asmx” to the file as in the following:

3. Write Down the Following code

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Services;
  6. namespace SimpleCalcService
  7. {
  8. /// <summary>
  9. /// Summary description for CalcService
  10. /// </summary>
  11. [WebService(Namespace = “http://tempuri.org/")]
  12. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  13. [System.ComponentModel.ToolboxItem(false)]
  14. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
  15. // [System.Web.Script.Services.ScriptService]
  16. public class CalcService : System.Web.Services.WebService
  17. {
  18. [WebMethod]
  19. public int Add(int num1, int num2)
  20. {
  21. return num1 + num2;
  22. }
  23. [WebMethod]
  24. public int Sub(int num1, int num2)
  25. {
  26. return num1 — num2;
  27. }
  28. [WebMethod]
  29. public int Mult(int num1, int num2)
  30. {
  31. return num1 * num2;
  32. }
  33. [WebMethod]
  34. public int Div(int num1, int num2)
  35. {
  36. return num1 / num2;
  37. }
  38. }
  39. }

The code above is pretty simple. We are just creating four web methods for Adding, Subtracting, Multiplying, and Dividing two numbers.

4. Testing The Web Service

Now you can test your service. Right-click on the CalcService.asmx file from Solution Explorer then select View in Browser, you will see your service is running as shown in the following figure.

Now if you click on the Service description then you will get the description about the service in XML, basically, you will see the WSDL

5. Creating a Windows Client

Add a new C# Windows application. Then design the form as shown in the following figure.

Click on the “Smart task panel” of the ComboBox then select Add Items -> Add, Subtract, Multiply and Divide.

Right-click on the References folder then select “Add Service reference” then

paste the WSDL file address in the Address Bar.

Click “Ok”.

6. Call the Web Service on the Click Event of the Button

Double-click on the “Calculate” Button, then enter the following code.

  1. private void button1_Click(object sender, EventArgs e) {
  2. int num1, num2;
  3. num1 = Convert.ToInt32(textBox1.Text);
  4. num2 = Convert.ToInt32(textBox1.Text);
  5. ServiceReference1.CalcServiceSoapClient proxy = new ServiceReference1.CalcServiceSoapClient(); //Creating proxy object of Web Service
  6. if (comboBox1.Text == “Add”) {
  7. MessageBox.Show(proxy.Add(num1, num2).ToString()); //Calling Add method of CalcWeb Service
  8. } else if (comboBox1.Text == “Sub”) {
  9. MessageBox.Show(proxy.Sub(num1, num2).ToString());
  10. } else if (comboBox1.Text == “Mul”) {
  11. MessageBox.Show(proxy.Mult(num1, num2).ToString());
  12. } else if (comboBox1.Text == “Div”) {
  13. MessageBox.Show(proxy.Div(num1, num2).ToString());
  14. }
  15. }

Run your Windows application. Enter numbers in textBox1 and TextBox2 then select “Operation” from the ComboBox then click on the “Calculate” button. You will see the output as shown in the following figure.

How it works

When you clicked on the calculate button, the proxy object of the web service will be created. Through that proxy object,

your application will communicate with the Calc Web Service. All calculations will be done on the webserver, the results will be returned to the client application.

Summary

In this article, you saw how easy it is to create a Web Service in ASP.Net and how to call and bind that service to the client application.

Download code from Github

https://github.com/gaikwadsameer/simplecalculatorwebservice-

Post a Comment

0 Comments