Tuesday, 30 April 2013


Web Service 

What is web service?

The term Web services describes a standardized way of integrating Web-based applications using the XML, SOAP(Simple Object Access Protocol) , WSDL(Web Services Description Language) and UDDI(Universal Description, Discovery and Integration) open standards over an Internet protocol backbone.
 XML is used to tag the data, SOAP is used to transfer the data, WSDL is used for describing the services available and UDDI is used for listing what services are available. Used primarily as a means for businesses to communicate with each other and with clients, Web services allow organizations to communicate data without intimate knowledge of each other's IT systems behind the firewall.

Web service Architecture:-




What is use of it?

Web services signal a new age of trivial distributed application development.
While Web services are not intended nor do they have the power to solve every distributed application problem,
They are an easy way to create and consume services over the Internet.
One of the design goals for Web Services is to allow companies and developers to share services with other companies in a simple way over the Internet.
Web services take Web applications to the next level.
Using Web services, your application can publish its function or message to the rest of the world.
Web services use XML to code and decode your data and SOAP to transport it using open protocols.
Using Web services, you can exchange data between different applications and different platforms.

Types of web services?

There are basically Two types of web services:-

  • SOAP(Simple Object Access Protocol)
  • REST(REpresentational State Transfer)

Web API is a development in Web services (in a movement called Web 2.0) where emphasis has been moving away from SOAP based services towards representational state transfer (REST) based communications. REST services do not require XML, SOAP, or WSDL service-API definitions.
Web APIs allow the combination of multiple Web services into new applications known as mashups.
When used in the context of Web development, Web API is typically a defined set of Hypertext Transfer Protocol (HTTP) request messages along with a definition of the structure of response messages, usually expressed in an Extensible Markup Language (XML) or JavaScript Object Notation (JSON) format.
When running composite Web services, each sub service can be considered autonomous. The user has no control over these services. Also the Web services themselves are not reliable; the service provider may remove, change or update their services without giving notice to users. The reliability and fault tolerance is not well supported; faults may happen during the execution. Exception handling in the context of Web services is still an open research issue, although this can still be handled by responding with an error object to the clients.

Understanding of SOAP:-
SOAP is a standard that specifies the format of messages used in Web services. SOAP messages are messages formatted in XML - the SOAP standard specifies what XML elements must be present, in what order, and what data types they can contain. A SOAP message is comprised of the following parts:
A SOAP Envelope,
An optional SOAP Header, and
A SOAP Body




The SOAP Body and (optional) Header are contained within the SOAP Envelope, as shown in the diagram to the left. As aforementioned, a SOAP Header is an optional piece of a SOAP message, and for simple Web services is rarely used. We'll save a discussion on SOAP Headers for a later article. For now, let's focus on the SOAP Envelope and SOAP Body.
The SOAP Envelope merely consists of the XML root element, which specifies the namespaces used in the SOAP message. consider checking out the The root element of a SOAP message is <soap:Envelope>.
Realize that in a Web service interaction there is a total of two messages: first, the client sends a SOAP message to the server, invoking some method. After this method has executed, the server returns a response, which includes the return value of the method. For the first message, sometimes referred to as the SOAP request message, the SOAP Body contains the name of the method the client wishes to call, along with the method's input parameters. In the second message, the SOAP response message, the server sends back in the body the return value of the method. (Even if there is no return value, a message is still sent back if, nothing else, to verify that the method executed.) The SOAP Body is signified by the <soap:Body> element.
The following is a sample SOAP request message:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <WebServiceName xmlns="Web Service Namespace">
      <inputParameter1>input parameter value</inputParameter1>
      <inputParameter2>input parameter value</inputParameter2>
      ...
      <inputParameterN>input parameter value</inputParameterN>
    </WebServiceName>
  </soap:Body>
</soap:Envelope>
And here is a sample SOAP response message:
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <WebServiceNameResponse xmlns="Web Service Namespace">
      <WebServiceNameResult>result value</WebServiceNameResult>
    </WebServiceNameResponse>
  </soap:Body>
</soap:Envelope>
The elements specific to the SOAP standard are in bold. Notice that the SOAP message begins with a <soap:Envelope> element, which contains a <soap:Body> element. Inside the <soap:Body> element of the SOAP request message is XML that specifies the Web service method to invoke and its input parameters. Inside the <soap:Body> element of the response SOAP message is the XML specifying the method that was called and its response, if one exists.


Let’s  Create Web  service 


  •  Web service for: - Getting all the information about the video
  • Table name :- eb_Video 
  •  Fields fo table :- (iVideo_ID, sVideo_Title, sVideo_Description,       sVideo_Duration, sVideo_ThumbImageurl, sVideo_url, sStatus, sVideo_RSTPurl,sVideoCode)

  • Create one parent folder in your application name “Webservice”.



  • Right Click on Folder -> Add new item - > webservices ->NameOfWebservicesPage (forex:- If you write MobileSync then it will take MobileSync.asmx)


Look into App_Code one class file will automatic generated  with same name.
(forex:- MobileSync.cs)

Make one class file  in App_code  for example here I am taking class file name like

       EtherBooks_Video.cs and  define few varibles like
          public string ErrorMessage = "";
        public bool IsError = false;
        public int TotalVideo;

public List<WS_Video> VideoList = new List<WS_Video>();

 ErrorMsg  :- if any error occure during the exection time we will display    it by ErrorMsg.
 IsError :- it retunrs True or False. If error is availble then we will set it as true else false.
TotalVideo :- it will return total amont of the record . in the place of vide you have to put your own varible.
Make one another class file for the data member declaration . (for example I am taking  WS_Video.cs )

public class WS_Video
{
 #region Data Member
 public string iVideo_ID, sVideo_Title, sVideo_Description,       sVideo_Duration, sVideo_ThumbImageurl, sVideo_url, sStatus, sVideo_RSTPurl,sVideoCode;
 #endregion
}
And this above class will declare in         EtherBooks_Video.cs
as a list.


Go in to MobileSync.asmx page

[WebMethod]
    public EtherBooks_Video GetVideos(int PageNumber, int   PageSize, string DeviceType)
    {
            // Do Coding call store procedure and return the values.
                   
    }
       
       
   
     

Ouput will look like this:-

         
       
       
Take another example of web service but here  we will display record into pagging
For that need to pass parameter like pagenumber , pagesize and devicetype
All the above steps are same just change comes into stored procedure.
    Example of stored procedure for the web service :-
First take all the values in temp table and after the use of it remove temp table

CREATE PROCEDURE [dbo].[EB_SP_RetrieveAllVideoDetail]      
 @PageNumber  INT,      
 @PageSize  INT,      
 @DeviceType varchar (25) = null      
AS      
BEGIN      
       
 -- Paging Logic      
 Declare @RowStart int      
 Declare @RowEnd int      
 declare @sql varchar(5000)      
 declare @temp varchar(10)      
       
 set @temp = CONVERT(varchar(10),@PageSize)      
 SET @PageNumber = @PageNumber -1      
     
 SET @RowStart = @PageSize * @PageNumber + 1;      
 SET @RowEnd = @RowStart + @PageSize - 1 ;      
     
     
  CREATE TABLE #TEMPVideoTable      
  (      
   iTempVideoID INT PRIMARY KEY IDENTITY(1,1),      
   iVideo_ID   BIGINT,      
   sVideo_Title  VARCHAR(200),      
   sVideo_Description VARCHAR(4000),      
   sVideo_Duration varchar(10),      
   sVideo_ThumbImageurl varchar(200),      
   sVideo_url varchar(200),      
   sStatus  varchar(15) ,      
   sVideo_RSTPurl varchar(200),
   sVideoCode varchar(100),    
   RowNumber int      
  )      
        if (@DeviceType = 'iphone')      
  begin      
     
   INSERT INTO #TEMPVideoTable (iVideo_ID,sVideo_Title,sVideo_Description,sVideo_Duration,sVideo_ThumbImageurl,sVideo_url,sStatus ,sVideo_RSTPurl,sVideoCode,RowNumber)      
       
       
   SELECT      
    V.iVideo_ID,      
    sVideo_Title,      
    sVideo_Description,      
    sVideo_Duration,      
    sVideo_ThumbImageurl,      
    sVideo_url,      
    sStatus,      
    sVideo_RSTPurl,  
    sVideoCode,  
    ROW_NUMBER() OVER (order by V.sVideo_Title DESC) as RowNumber      
         
   FROM      
    EB_Video V      
         
   WHERE      
         
    V.sStatus LIKE 'Active'      
         
     
  -- Returns records      
  SELECT *      
  FROM #TEMPVideoTable      
  WHERE iTempVideoID >= @RowStart AND iTempVideoID <= @RowEnd        
       
  -- Return no. of records      
  SELECT Count(iVideo_ID) as iTotalRecords      
  FROM      
   #TEMPVideoTable      
     
 DROP TABLE #TEMPVideoTable        
  end      
       
     
 end      
       
         
   ---- Returns records      
         
   -- SELECT  * FROM Books WHERE RowNumber >= @RowStart AND RowNumber <= @RowEnd order by iBookID desc

No comments:

Post a Comment