Share Blog

Saturday, May 03, 2014

How to Use Web Services in ASP.NET application

Web Services is service which is available over the web. A Web Service is used to implement the specific functionality to Execute the business logic codes of  the application . This technologies allowed  to developer to create a specific component once (make only logic code) so that they can share it across the multiple application by multiple developers. Before the Web Services , we were using two technology which are given below:
·    Component object model (COM)
·     Distributed Component object model (DCOM)
This technology is helpful to use one application to another application. Web Services overcome these difficulties as they can be shared (distributed) among multiple website without the need to install them on each individual client machines.
There are some advantages of web services over COM and DCOM Technologies.
·     Web Services are very simple in use.
·     Web Services do not carry any state information,so that multiple request can be processed simultaneously.
·    Web Services are enforcing trusted connection between the server and the client.

Examples of Web Services 

      1). Preparing the pay slip for employee by a company 
     3. )  Display the weather report on the  website
     4. ) Calculate the simple interest and income tax of the banks and income tax departments.
     5. ) Display the New News (Notification) Headlines on the website.

we can use different website (asp.net ,php, java, etc) and different web services for performing any task. We can use any web services in different platform.Web services are used XML language for communication to the website .You already know XML is platform independent .

1.     1 )  HTTP :- It is known as  Hyper Text Transfer Protocol .All Internet protocols follow some rules.It is a connection less protocol.This protocol is used to transmit the data between web services and it users (website).
2.     2 )  XML :- It is known as Extensible Markup Language.It is a standard used by all web services to transfer the data.It is platform independent.So that e can use web services on any website without facing platform compatibility problem.
3.      3. )  WSDL :- It is Known as Web Service Description Language.It is a standard used by the web service to describe what a web service can do. WSDL is a XML-based language that defines web services.
There are some elements in WSDL Document which are given below:-
       1.Type :- It describe the all data types used in web service.
     2.Message :- It is describe the message which are used to communicate from one point to another when data is passed by the web service
      3.Binding : - It describes the all protocols which are used by the web service
      4.Port Type :- It describes the links of input and output message
     5.Port :- It describe the address for binding the specific client port (end user port).
     6.Service :- It describes all services such as binding URL ,Port etc. 


1.    4  )  SOAP :-It is known as Simple object access protocol.It is a standard used by the web service to send the response (or request) message to the application (website).It is completely XML based protocol.SOAP provides a complete set of rules for messaging.One of the great advantage of using  a SOAP message is that ,it is not restricted to any one operating system.
2.     5 )  DISCO.exe :- It is known as the web service discovery tool,It is used for publishing and discovering web services.This tool is helpful to discover URL of all the web services located on a web server and creates a list of them in a file,called DISCO file (with .disco extension).
6. )  UDDI :- It is known as Universal Description,Discovery and Integration .UDDI includes four types of services.


·     Web service
·        Business
·     Binding
·      Specifications
UDDI is a central Directory where web services offered by different organization are published.It is Built into the .net platform.It is platform Independent.
eatures of UDDI


·      It stores the information of web services.
·     It uses W3C & IETF standards (ex. XML,HTTP,DNS etc.).
·     It uses SOAP for communication.
·     It uses WSDL to describe interfaces to web services.
Advantages of Web Services 


·     It enhance the security feature of any website.
·      It takes less processing time to calculate the logic code.
·       It can easily handle multiple request at a time simultaneously.
·        It is compatible to communicate with different platform.
·         It follows XML ,HTTP Standards.           

Step -1 First open your visual studio --> File -->New -->Select ASP.NET Empty Website and select Visual C# from left window -->OK-->Now Open Solution Explorer-->Now Right click on website --> Add New Items --> Select web service & Visual C# --> Write your web service's name (calculationservices.asmx) --> click Add button as shown below:-
Step -2 Now Write the C# logic codes for calculation under [WebMethod] as given below:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for calculationService
/// </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 calculationService : System.Web.Services.WebService {

    public calculationService () {

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

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod]
    public int add(int i, int j)
    {
        return i + j;
    }

    [WebMethod]
    public int substract(int i, int j)
    {
        return i - j;
    }
    [WebMethod]
    public int multiply(int i, int j)
    {
        return i * j;
    }

    [WebMethod]
    public int Division(int i, int j)
    {
        return i / j;
    }

  
    

}

Step -3 Now Run this web services (calculationservices.asmx) -->Now copy the Browser's URL as shown below:-


Step -4 Now you can calculate your services(Addition,Subtraction,Multiplication,Division and modulus ) -->click Division link or others as show below:-

Step -5 Now click Invoke Button -->you will see ,output will show in XML Document format using SOAP Protocol
Note :- You can calculate (test) all result  using this method, your service is working or not.


Step -6  Now open your visual studio again--> File -->New -->Select ASP.NET Empty Website and select Visual C# from left window -->OK-->Now Open Solution Explorer-->Now Right click on website --> Add New Web Form (webservice.aspx) --> Drag and drop Label ,Text Box and Button control on the Form as shown below:- 


Note :-

·    web service extension --> .asmx
·     Web Form Extension -->.aspx
·     Disco File Extension -->.disco
Step -8 Now Write the web service'Name (myservice) -->Click Add Reference Button.

as shown below :-

Step -9 You will see ,there are some files are added in your application(when we complete the web service configuration) as shown below:-

Note:- See above image carefully:-
·      Include a Name space in webservice.aspx.cs file as Using myservice;
·Create object of calculationservices in webservice.aspx.cs file as calculationservices obj = new calculationservices();
Step -10 Now write the c# codes on button click in webservice.aspx.cs file as given below:-


using System;
using myservice;

public partial class myservices : System.Web.UI.Page
{
    calculationService obj = new calculationService();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int sum = obj.add(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
        Label1.Text = "Your addition Result =" + sum.ToString();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
     int sub = obj.substract(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
        Label1.Text = "Your Substraction Result =" + sub.ToString();

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
     int mult = obj.multiply(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
        //Response.Write("<script> alert(" + mult + ")</script>");
        Label1.Text = "Your Multiplication Result =" + mult.ToString();

    }
    protected void Button4_Click(object sender, EventArgs e)
    {
     int div = obj.Division(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
        Label1.Text = "Your Divison Result =" + div.ToString();

    }

}
Step -11 Now Run the Application (webservice.aspx) (press F5) -->Enter the Text Box Fields -->press each button( Add, Subtract......... )as shown below:-



No comments:

Post a Comment