Thursday, March 24, 2022

Creating and Using Web Services

What is Web Service? 

A Web Service is a reusable piece of code used to communicate among different Applications. Once a web service is created and hosted on the server in the internet it can be used by any application developed.

A file with .asmx extensions is an ASP.NET Web Service file that provides communication between two objects over the internet using the Simple Object Access Protocol (SOAP).

Creating and Using Web Services

Steps to create web service:

Step 1: Open .Net Visual Studio, create an Empty web site.

Step 2: Right click on App location in Solution Explorer and Select “Add New Item”, from add new item dialog box select and add “WebService” file with “.asmx” extension.
This file contains some program line as given below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
   
}

From above line of code the part 

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
Containing the methods used as web services. You can add your own methods, like

[WebMethod]
    public int MyCalc(int x, int y)
    {
        return x * y;
    }
Accordingly go on adding methods as per the need of our web application.

Step 3: To test these newly added methods just execute the project, it will run in web browser, and the following output will be observed. After Executing it will display the names of web services.





To execute these services just click on the name of the web service. The output screen of “MyCalc” web service can be shown as

As MyCalc service requires two parameters, it will ask for two values.

Making the use of web service in the Web Application:

Step1: To make use of this Web Service add an Web Form (i.e. Default.aspx page) in the project.

Step 2: Right click in solution explorer, select “add web reference”, this will open Add Web Reference dialog box, from the dialog box, Click on “Select Web Services in this solution” as shown in the figure.



Step 3: From the dialog box above click on the Add Reference button. This will add “.disco” and “.wsdl” files in the application.
(Note: WSDL: WSDL stands for Web Service Description Language. It is written in XML and also specifies the location of the service and the methods used.
 
XML grammar describes details such as:-
1. Where we can find the Web Service (its URI)?
2. What are the methods and properties that service supports?
3. Data type support.
4. Supported protocols

The DISCO (is the abbreviated form of Discovery) file typically points to a WSDL(Web Service Description Language) source that in turn points to the actual Web Service.When one searches for the Web Services, it will go to the DISCO which gives actual information for the particular Web services. It has .disco file extension. It is an XML document which contains link to another resources.)

 
Step 4: Now add two textbox controls and a button control, and on the click event of button control add the following line of code in code behind model of the Default.aspx file.

        localhost.WebService wc = new localhost.WebService();
        int a = int.Parse(TextBox1.Text);
        int b = int.Parse(TextBox2.Text);
        int c = wc.MyCalc(a, b);
        Label1.Text = "Answer is " + c.ToString();


1 comment: