How to Use Web Services in ASP.NET application

By
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.However ,there were some fundamental difficulties in using these components. One such difficulty was that these components needed to be physically distributed and Explicitly registered on each client machine.
Today,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.
  • Web Services can easily use in caching feature of asp.net.
  • With the help of Web Services,we can easily integrate asp.net web application with third party systems.
  • Web Services is useful in security purpose as ssl and window authentication of asp.net application.
Examples of Web Services 
There are some example of Web Services which are given below:-
     1. ) Suppose we want to host a website(asp.net,php ,java etc) on the server that calculate the simple interest.For this, we can use a web service to calculate the simple interest.In this scenario,we do not need to write  codes  in the website for calculating the simple interest. We do not put calculating simple interest code inside the website,for this we can create a Web Service which can calculate the simple interest.Here Our website performs two things,
  • Take input from client side (end user) using Text Box.
  • Display the output to the client end ( Calculating the simple interest by the Web Service).
     2. )  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.

Real Life Example of Web Services
I think almost every people know about TV and TV channels. There are many company which manufactures TV as
  • Sony
  • Samsung
  • LG
  • Vediocon 
  • Others
There are some service providers (TV Channels) which provide the services as
  • Airtel Digital
  • Dish
  • Reliance Digital
  • Tata sky
  • Vediocon d2h
  • Sun direct
  • others
Similarly we can compare TV --to-> Website and TV Channels (services) --to->Web Services.
Here peoples are using different TV and Channels to use the services.They are not facing any type of problems by using this different TV and TV Channels.Same concepts are used in website and web services also. Similarly 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 .
There are some standard used by web services in ASP.NET which are given below:-
     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),Such as HTTP-GET and HTTP-POST.A web service can use any remote procedure call (RPC) protocols,such as DCOM (Distributed component object model ) or Common object Request Broker Architecture (CORBA),to transmit data HTTP-GET and HTTP-POST protocols used by the web services to transmit the data over the Internet.By default HTTP-GET and HTTP-POST bindings are not enabled to Remote web service invocation.
  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.
Note:-You can define an XML web service as a unit of code that can be invoked via HTTP Requests.
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.Every web service holds WSDL document that specifies the location of the web service and list of all the services that the web service can perform. A WSDL document is a simple XML document that containing different XML elements to define  a web service. I will show you all these standard when i will implement web service in below section of this page.
There are some elements in WSDL Document which are given below:-
  • Type :- It describe the all data types used in web service.
  • Message :- It is describe the message which are used to communicate from one point to another when data is passed by the web service.
  • Binding : - It describes the all protocols which are used by the web service.
  • Port Type :- It describes the links of input and output message.
  • Port :- It describe the address for binding the specific client port (end user port).
  • Service :- It describes all services such as binding URL ,Port etc. 
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.
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).
Note:- This file mostly can be accessed by clients (users) to know about these kinds of services an organization provides.
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.
Features 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.
In this tutorial i am going to implement web service at different ways in .NET Application.Which are given below:-
How to call web services from the client end
There are some steps to implement web service concepts in asp.net application.In this application ,i have create a web service which can calculate Addition,Subtraction, Multiplication, Division and modulus operations.When user put two inputs on the website using Text Box then this web service calculate the operations(Addition,Subtraction........) and send output to the website(client end),after that website display that output in script or Label.
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:-


create_service

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 calculationservices
/// </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 calculationservices : System.Web.Services.WebService {

    public calculationservices () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    [WebMethod]
    public string msdotnet()
    {
        return "welcome to www.msdotnet.co.in";
    }
    [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;
    }

    [WebMethod]
    public int modulos(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:-


run_service

Note :- You can paste this URL on Notepad,MS Word etc. so that you can use this URL ,when we will configure the web service on website.

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


calculation


Step -5 Now click Invoke Button -->you will see ,output will show in XML Document format using SOAP Protocol.


SOAP _out

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


Web Form

Note :-
  • web service extension --> .asmx
  • Web Form Extension -->.aspx
  • Disco File Extension -->.disco
Step -7 Now open your solution Explorer -->Right Click on your application (website) -->Add Web Reference -->Paste web service URL here (Which i have already copied in step 3) -->Click Next Arrow as show below:-


URL

Note :- You can copy the URL by Run the web service again.

Step -8 Now Write the web service'Name (myservice) -->Click Add Reference Button.
as shown below :-
webservice_name

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


we_files

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 System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using myservices;
public partial class webservice : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        calculationservices obj = new calculationservices();
        //you can use below without include Namespace(using myservice;)
        //myservices.calculationservices obj = new myservices.calculationservices();
        int sum =obj.add(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
        Label1.Text = "Your addition Result =" + sum.ToString();
        //you can use below script instead of Label control
        //Response.Write("<script> alert(" + sum + ")</script>");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        calculationservices obj = new calculationservices();
        int sub = obj.substract(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
        Label1.Text = "Your Substraction Result =" +sub.ToString();
        //you can use below script instead of Label1 control
        //Response.Write("<script> alert(" + sub + ")</script>");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        calculationservices obj = new calculationservices();
        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)
    {
        calculationservices obj = new calculationservices();
        int div = obj.Division(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
        Label1.Text = "Your Divison Result =" + div.ToString();
        //you can use below script instead of Label1 control
        //Response.Write("<script> alert(" + div + ")</script>");
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        calculationservices obj = new calculationservices();
        int mod = obj.modulos(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
        Label1.Text = "Your Modulos Result =" + mod.ToString();
        //you can use below script instead of Label control 
        //Response.Write("<script> alert(" + mod + ")</script>");
    }
}
Step -11 Now Run the Application (webservice.aspx) (press F5) -->Enter the Text Box Fields -->press each button( Add, Subtract......... )as shown below:-
web service _output

For More....
  1. How to implement caching in asp.net website
  2. How to use validation controls in asp.net
  3. How to insert Edit,update,delete and delete records in Data List control
  4. How to send forget password to your registered email using asp.net free
  5. How to implement session in asp.net website
  6. Threading in c#
  7. How to use navigation control in asp.net
  8. File handling Real application
  9. Collections in C#
  10. How to implement Stored procedure in MS Sql server
  11. Example of Delegate in c#
  12. Asp.net coding Models
Note :- Other web services and latest WCF services,I will explain in coming our tutorials.Then i will tell you,what is difference between them also.
Download whole attached file
       Download

5 comments:

  1. A lot of thanks for helping me...............

    ReplyDelete
  2. Very intresting and detailed information... very nice ..keep it up.. :)

    ReplyDelete
  3. very helpful thanks a lot...............

    ReplyDelete
  4. how can i access third party webservice without adding web reference using httpwebrequest..pls help me thanks in advance

    ReplyDelete
  5. how can I hide the check-list item at the time of page loading,if value of checklist does not match with particular condition ?? I am using C#.net

    ReplyDelete

Powered by Blogger.