Basic of Web service

Saturday, 27 August 2016

Basic of Web service

What is Web Service ?

A Web service is a software system identified by a URI, whose public interfaces and bindings are defined and described using XML Learn Details of XML  )
. Its definition can be discovered by other software systems.These systems may then interact with the Web service in a manner prescribed by its definition, using XML based messages conveyed by internet protocols
Web service architecture picture is shown below:



 Web Services are platform independent. Service written in any language can communicate with service of other languages. Web service support  the following  transporting protocol Internet Protocol,HTTP, STMP. Web service uses XML as common language  ( Learn Details of XML  ) to communicate over network between two different application. As a result, the set of data types Web Services can use is limited to the set of data types recognized by the XML Schema ( Learn Details of XML  )standard. So using service we can transport simple data (strings and numbers, object,arrays) and we can't send .Net object like FileStream, an Image. If you want to transport .net( like FileStream, an Image) object to the client then client should be in .net platform.

Web service supported data types are
• Primitive data types (The Basics): The built in C# data types such as short, int, long, ushort, uint,          ulong, float, double, decimal, bool, string, char, byte and DateTime.
• Classes and structure: The Classes and structure with serialize public fields or property.
• Arrays: Arrays of any supported type (built-in or custom objects). You can also use an ArrayList         (that is simply converted into an array).
• Enum types: Enums are supported.
• XmlNode: Xmlnode types can be passed as a parameter or return values.
• DataSet : DataSet are allowed. .

Now To create a web service using visual studio do the following steps:

First open a visual studio and create a web site project. If you are using VS2010 or above then Change the framework version to 3.5. Now add the Web service with a name. This picture will give the details information how to do the above functionality.


Create A web service in a web application

After creating web service it will generate automatically a code behind file which shown in below picture

The MyService class has a method HelloWorld that is decorated with a “[WebMethod]”         attribute. The methods of the service that are to be accessed by the client  application should be decorated with this attribute. There may be some method that the service is using for some internal functionality, client applications don't need to access them. Don't decorate such methods with a WebMethod attribute.
Instead of using Project if you create a website then with in web service asmx page you will find the below tag. Using both the way you can create Web service.
1. <%@ WebService Language="VB" CodeBehind="~/App_Code/MYService.vb" Class="MYService" %>

• The “WebService” directive, indicates this asmx page is a web service.
• The “Language="C#"”, is to indicate language used for this service.
• The “CodeBehind” property is nothing to do with ASP.NET or web service, this is completely a Visual Studio property, that is used to map the asmx page with it's code behind page.


Now, run your application by hitting F5, http://..../MYService.asmx will open in your browser (the port number may vary). On the top of the below image you will find a  link "click here for complete list of operation", that will redirect to the WSDL document ( Learn Details on WSDL ) of the service, another link for HelloWorld (list for methods exposed by service) that will redirect to a page  for testing this method (In this picture the localhost tab containing the result details.).