This article will explain you everything about Web Services in .Net, so let’s
get started with Web Service
What is Web Service?
What is Web Service?
Web Service is an application that is designed to interact directly with
other applications over the internet. In simple sense, Web Services are means
for interacting with objects over the Internet.
- Language
Independent
- Protocol
Independent
-
Platform
Independent
-
It assumes stateless
service architecture.
We will discuss more on web service as the article
proceeds. Before that let’s understand bit on how web service comes into
picture.
History of Web Service or How Web Service comes into existence?
As i have
mention before that Web Service is nothing but means for interacting with
objects over the Internet.
- Initially Object Oriented Language comes which allow us
to interact with two object within same application.
- Comes to Component Object Model (COM) which allows interacting
two objects on the same computer, but in different applications.
- Comes to Distributed Component Object Model (DCOM)
which allows interacting two objects on different computers, but within same
local network.
- And the web services, which allows two object to
interact internet. That is it allows interacting between two objects on
different computers and even not within same local network. Examples: weather
reporting, stock market, news headlines etc.
Web Service Communication
Web Services communicate by using standard web protocols and data formats, such as
-
HTTP
-
XML
-
SOAP
Advantages of
Web Service Communication
Web Service messages are formatted as XML, a standard way for communication between two incompatible systems. And this message is sent via HTTP, so that they can reach to any machine on the internet without being blocked by firewall.
Terms which are frequently used with web services?
Web Service messages are formatted as XML, a standard way for communication between two incompatible systems. And this message is sent via HTTP, so that they can reach to any machine on the internet without being blocked by firewall.
Terms which are frequently used with web services?
What is SOAP?
SOAP is remote
function calls that invokes method and execute them on Remote machine and
translate the object communication into XML format. In short, SOAP are way by
which method calls are translate into XML format and sent via HTTP.
What is WSDL?
WSDL stands for
Web Service Description Language, a standard by which a web service can tell
clients what messages it accepts and which results it will return. WSDL
contains every details regarding using web service
-
Method and
Properties provided by web service
-
URLs from which
those method can be accessed.
-
Data Types used.
-
Communication protocol
used.
What is UDDI?
-
UDDI allows you
to find web services by connecting to a directory.
What is Discovery or .Disco
Files?
-
Discovery files
are used to group common services together on a web server.
-
Discovery files
.Disco and .VsDisco are XML based files that contains link in the form of URLs
to resources that provides discovery information for a web service.
.Disco File (Static)
.Disco File
contains
-
URL for the WSDL
-
URL for the
documentation
-
URL to which
SOAP messages should be sent.
-
A static
discovery file is an xml document that contains links to
other resources that describe web services.
.VsDisco File (Dynamic)
-
A dynamic
discovery files are dynamic discovery document that are automatically generated
by VS.net during the development phase of a web service.
What is difference between Disco and UDDI?
-
Disco is
Microsoft's standard format for discovery documents which contains information
about Web Services, while UDDI is a multi-vendor standard for discovery
documents which contains information about Web Services.
What is Web Service Discovery Tool (disco.exe)?
-
The Web Services
Discovery Tool (disco.exe) can retrieve discovery information from a server
that exposes a web service.
What is Proxy Class?
-
A proxy class is
code that looks exactly like the class it meant to represent; however the proxy
class doesn't contain any of the application logic. Instead, the proxy class
contains marshalling and transport logic.
-
A proxy class
object allows a client to access a web service as if it were a local COM
object.
-
The Proxy must
be on the computer that has the web application.
What is Web Service Description Language Tool (wsdl.exe)?
-
This tool can
take a WSDL file and generate a corresponding proxy class that you can use to
invoke the web service.
-
Alternate of
generating Proxy class through WSDL.exe is you can use web reference. Web
Reference automatically generates a proxy classes for a web service by
setting a web reference to point to the web service.
Advantage of using Web Reference as compare to using WSDL.exe Tool is
you can update changes done in web service class easily by updating web
reference, which is more tedious task with WSDL.exe tool.
Testing a Web Service?
-
You can test web
service without building an entire client application.
-
With Asp.net you
can simply run the application and test the method by entering valid input parameters.
-
You can also use
.Net Web Service Studio Tool comes from Microsoft.
Example of Creating Web Service in .Net
This Web Service will retrieve Customer List Country Wise and return as dataset to client application for display.
Step1: Create a Web Service Application by File > New > Web Site > Asp.net Web Services
Named the web service, for example here i have chosen name "WSGetCustomerCountryWise"
Step2: Rename the default Service.asmx file to proper name, you also need to switch design view and change the class name with the same name you use to rename the service.asmx.
This Web Service will retrieve Customer List Country Wise and return as dataset to client application for display.
Step1: Create a Web Service Application by File > New > Web Site > Asp.net Web Services
Named the web service, for example here i have chosen name "WSGetCustomerCountryWise"
Step2: Rename the default Service.asmx file to proper name, you also need to switch design view and change the class name with the same name you use to rename the service.asmx.
For example, "WSGetCustomerCountryWise.asmx" and switch to design view and change the class="Service" to class="WSGetCustomerCountryWise"
Step3: Rename the Service.CS File to proper name, you need to change the class name and constructor name too.
For example, "WSGetCustomerCountryWise.CS" and switch to code view
and change the class and constructor name to
"WSGetCustomerCountryWise"
After three steps your solution explorer looks as shown in figure:
Step4: Create a Logic for Web Service
After three steps your solution explorer looks as shown in figure:
Step4: Create a Logic for Web Service
-
Create a Method
"GetCustomerCountryWise"
-
Note: You need
to specify [WebMethod] before method definition, if you want it to be
accessible public, otherwise the method would not be accessible remotely.
-
Specify proper
argument and return type for method in web service.
-
It is also good
practise to specify the use "Description" attribute to tell what
method is meant for.
For example,
here i need to access data of northwind customers and want to return customer
list country wise, so add namespace for
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
[WebMethod]
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
[WebMethod]
public
System.Xml.XmlElement GetCustomerCountryWise(string sCountry)
{
string sConn = ConfigurationManager.ConnectionStrings["connStr"].ToString();
string sSQL = "select CustomerId, CompanyName, ContactTitle, City " +
" from Customers where country = '" + sCountry + "'";
SqlConnection connCustomer = new SqlConnection(sConn);
DataSet dsCustomer = new DataSet();
SqlDataAdapter daCustomer = new SqlDataAdapter(sSQL, sConn);
daCustomer.Fill(dsCustomer,"Customers");
//To access data we need to make use of XmlElement.
// Return the DataSet as an XmlElement.
System.Xml.XmlDataDocument xdd = new System.Xml.XmlDataDocument(dsCustomer);
System.Xml.XmlElement docElem = xdd.DocumentElement;
return docElem;
}
Step5: Build Web Service and Run the Web Service for testing by pressing F5 function key.
{
string sConn = ConfigurationManager.ConnectionStrings["connStr"].ToString();
string sSQL = "select CustomerId, CompanyName, ContactTitle, City " +
" from Customers where country = '" + sCountry + "'";
SqlConnection connCustomer = new SqlConnection(sConn);
DataSet dsCustomer = new DataSet();
SqlDataAdapter daCustomer = new SqlDataAdapter(sSQL, sConn);
daCustomer.Fill(dsCustomer,"Customers");
//To access data we need to make use of XmlElement.
// Return the DataSet as an XmlElement.
System.Xml.XmlDataDocument xdd = new System.Xml.XmlDataDocument(dsCustomer);
System.Xml.XmlElement docElem = xdd.DocumentElement;
return docElem;
}
Step5: Build Web Service and Run the Web Service for testing by pressing F5 function key.
By pressing "Invoke" button will generate XML File. So you are done creating web service application.
Example of Testing Web Service in .Net
This Web Service will display the information which had been retrieved from Remote computer by accessing public method "GetCustomerCountryWise".
Step1: Create a Test Web Site by File > New > Web Site > Asp.net Web Site
Named the web site, for example here i have choosen name "TestGetCustomerCountryWise"
Step2: Displaying data in gridview, so drag the gridview on to the form.
Step3: Right Click Solution Explorer and Choose "Add Web Reference"
Step4: Choose the option Web Service on the local machine or you can enter the .WSDL File address directly in URL space and press Go button.
Step5: Press "Add Reference button"
Step6: Writing Code for Displaying data in GridView
Here note: I have Pass "USA" as parameter in Country Field.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Create object of WSGetCustomerCountryWise Object
localhost.WSGetCustomerCountryWise objGetCustomerCountryWise
= new localhost.WSGetCustomerCountryWise();
DataSet dsCustomer = new DataSet();
// Get the data from Webservice.
XmlElement elem = objGetCustomerCountryWise.GetCustomerCountryWise("USA");
// Load the XML to the Typed DataSet that you want.
XmlNodeReader nodeReader = new XmlNodeReader(elem);
dsCustomer.ReadXml(nodeReader, XmlReadMode.Auto);
GridView1.DataSource = dsCustomer;
GridView1.DataBind();
}
}
Step7: Output as data displayed in GridView.
Few Facts about Web Service in .Net
{
if (!Page.IsPostBack)
{
//Create object of WSGetCustomerCountryWise Object
localhost.WSGetCustomerCountryWise objGetCustomerCountryWise
= new localhost.WSGetCustomerCountryWise();
DataSet dsCustomer = new DataSet();
// Get the data from Webservice.
XmlElement elem = objGetCustomerCountryWise.GetCustomerCountryWise("USA");
// Load the XML to the Typed DataSet that you want.
XmlNodeReader nodeReader = new XmlNodeReader(elem);
dsCustomer.ReadXml(nodeReader, XmlReadMode.Auto);
GridView1.DataSource = dsCustomer;
GridView1.DataBind();
}
}
Step7: Output as data displayed in GridView.
Few Facts about Web Service in .Net
-
Each Response
from web service is a new object, with a new state.
-
Web Service is
asynchronous because the request object from the client application and the
response object from the web service are unique SOAP envelopes that do not
require shared connection.
-
This allows
client application and the web service to continue processing while the
interaction is on-going.
-
Instead of a
user interface, it provides a standard defined interface called a contract.
thanks sir..its very helpful..keep on posting these type of topics..
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteNice explanation.... Worked well... Thanks...
ReplyDelete