Wednesday, 17 July 2013

What is Web Service in .Net

What is Web Service?

W3C Definition for a Web Service is   “a software system designed to support interoperable machine-to-machine interaction over a network”.

It is an application that is designed to interact directly with other applications over the internet. Means Web Services is application which used to connect distributed application over the  web. The Web service consumers are able to invoke method calls on remote objects by using SOAP and HTTP over the Web. Web Service is language independent and Web Services communicate by using standard web protocols and data formats, such as
  • HTML :  It is used to display  the data
  • XML  :  It is  used to transfer the data
  • SOAP
What is SOAP? : Simple Object Access Protocol (SOAP) is a standard protocol specification for message exchange based on XML. Communication between the web service and client happens using XML messages. SOAP defines the rules for communication like what are all the tags that should be used in XML and their meaning. .  This is because it is XML based and language and platform independent.  SOAP supports any transport protocol and use Hyper Text Transfer Protocol (HTTP), File Transfer Protocol (FTP), Simple Mail Transfer Protocol (SMTP) and Post Office Protocol 3 (POP3) to carry documents.

Structure of SOAP Message:  It consists of three parts: an envelope, one or more headers and a body.

Envelope It root element which identifies the following:
·         Message content
·         Desired recipients
·         Special processing information

Header
This is an optional part of the SOAP message.  It lets the application exchange information.  It even enables identification of the features in the message and explains whether a feature is optional or mandatory.  This also provides processing information and furnishing additional products for some specific tasks such as security or transaction management. (We can use SOAP header to pass along username and password information so that only the users we choose can access the service.)

Body
The Body is the most important part of a SOAP message; rather we may say that it is the mandatory part of the SOAP message.  If the SOAP message contains no header, it is the first element after the envelope.  It contains the XML information being exchanged.  In a Web Service interaction there is SOAP request message and SOAP response message.  In case of SOAP request message, the client sends a SOAP message to the server, invoking some method


Web Services Description Language (WSDL) 

Web Services Description Language (WSDL) is an open standard language used in conjunction with XML-based languages.  Any application is running the web, which type of method used and what is the return type both maintained by WSDL.A WSDL file is an XML document that describes a set of SOAP messages and how the messages are exchanged.

Universal Description, Discovery, and Integration (UDDI)

Universal Description, Discovery, and Integration (UDDI) is an open, Internet-based specification that is the building block on which businesses may quickly and easily locate desired services and perform business with one another.

What is Disco File..

 It stands for Discovery files which stores the information related to the .Net Frame Work Services.  In file same type services which are grouped by Disco file…….

Type of Web Service:
·         One way
·         Request-Reply

Main Feature of Web Services
·         Language   independent
·         Platform  independent


Note:  In Remoting concept  in c# .net you can use only homogeneous application means java to  java but  in  web services you can  use different application …..

 Example  with Web Services………………………………………………
 
 First we take   Asp.Net Empty Website then 
 add  web services .....



Step by step…


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();
    }
    //this  method  is showing here by default
    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
    //we create another method  here..
    [WebMethod]
    public string evenodd(int a)
    {
        if (a % 2 == 0)
            return "No  is  Even";
        return "No is Odd";
    }
   
}

  then we check our method




  How to Call above function in another Website with help Web Service

First we run   WebService.asmx of website1 and copy its url from web browser ………………

We take another website  which name  Website2… and call above services in this  website………
       1)    First right click  at Website2
       2)    Click on the “Add Web Reference”  link
       3)    Now paste above URL into the new window.
       4)    Press the GO button, just to check your  web service working or not
       5)    Now click on the “ADD REFERENCE” button






We take one web Form Default.aspx   and call both method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
// call method with web Service.....................
protected void btnMethod_Click(object sender, EventArgs e)
{
localhost.WebService w = new localhost.WebService();
Label1.Text=w.HelloWorld();
}
protected void btnResult_Click(object sender, EventArgs e)
{
localhost.WebService w = new localhost.WebService();
Label1.Text = w.evenodd(Convert.ToInt32(txtno.Text));
}
}


0 comments:

Post a Comment