Web Services


Web Services

WEB SERVICES PROVIDE A way to run a service on the web and access its methods using standard protocols, including Simple Object Access Protocol (SOAP), Extensible Markup Language (XML), Web Service Description Language (WSDL), and Hyper Text Transfer Protocol (HTTP).

 

Technically a Web Service is nothing more then an application that exposes its interfaces to a client who wants to access the service's abilities.

 

The uses of a web services include validating credit cards, searching for data in a database, inserting an order into a shopping carts and updating a guest list.

Web Services under .NET run by invoking methods in the service directly through HTTP or SOAP, so someone wanting to run your Web Service from their computer at home can simply send an HTTP call to your service, passing the parameters in a standard Uniform Resource Locator (URL).

 

Three attribute to make a Web Service:

Discovery: First, we need to locate the Web Service. We locate the Web Service through the Discovery Service Protocol. The *.disco file stores the protocol in your Visual Studio (VS) .NET project. This file contains references to all the Web Services under our Web site's virtual directory. VS can automatically generate this file for you. The discovery file (.vsdisco), an XML file, is used to identify searchable paths on the web server used by discovery process.

 

Description: Once we have discovered our service, we need a way to tell the client what methods, classes and so on the Web Service has and what writing protocol (SOAP, HTTP etc.) the services are using. We do this through WSDL, an XML format. VS provide tools for generating WSDL files automatically from your services.

 

Writing Protocol:  Web Services under the .NET use two main protocols. HTTP-post/ HTTP-get and SOAP. HTTP-post and HTTP-get enable you to send and receive information via a URL by passing and receiving name value pair strings. HTTP can only pass strings that represent different data types. SOAP allows you to pass a richer type of information, such as datasets.

 

Exploring Web Services and the .NET Framework Library

 

The .NET Framework class library provides four namespaces that contain Web Services classes. These namespaces are:

System.Web.Services

System.Web.Services.Description

System.Web.Services.Discovery

System.Web.Services.Protocol

 

System.Web.Services namespace provides classes that enable us to build and use Web Services. It has four classes. The WebService class defines the optional base class for Web Services, which provide direct access to an ASP.NET application. Server, Session, User and context are some of its properties.

WebMethodAttribute, WebServiceAttribute and WebServiceBindingAttribute are other classes of this namespace.

The System.Web.Services.Description namespace provides classes that enable you   to describe a WSDL.

The System.Web.Services.Discovery namespace provides classes the enable Web service consumers to locate a Web service through discovery.

 

The System.Web.Services.Protocol namespace provides classes that define the protocol used to transmit data between a client and a Web service.

 

 

Creating a Web Service in VS

 

To understand Web Services better, I'll show you how to build a simple Web Service. The Web Service will receive an order ID and then query the Northwind database's Orders table for the correct order. It'll then return the order in a DataSet.

 

The first step in creating the Web Service is to select File->New->Project and choose the ASP.NET Web Service template, as shown in following figure:



Figure 1.

 

This creates a Web Services project on Web server. By default, localhost is available as a Web Server on your development machine. If we look the solution explorer then we see that a list of files created automatically for the project, as shown in following figure:



Figure 2.

 

The web.config file is an XML file containing information on how to run the service under ASP.NET.

The service.asmx file servers as the entry point into the service. The code behind the service.cs is where we will place the method for retrieving the order. If we view the service.cs then initial code we find there:



Figure 3.

 

Here we find a method HelloWorld by default in service.cs page. Here I will use a new method. Before write any code, we need to add the [WebMethod] attribute at the start of this method to recognize it as a Web Method. Here in this example I simply create a connection and a data adapter to the orders table in the Northwind database. If we want to give some description below the method name at run time then we have to write before method.
 

[WebMethod(Description="Showing the Northwind Data")]

The Service.cs code is:

using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Data;

using System.Data.SqlClient;

 

 

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService

{

    public Service ()

   {

        //Uncomment the following line if using designed components

        //InitializeComponent();

   }

 

    SqlConnection con;

    SqlCommand cmd = new SqlCommand();

    SqlDataAdapter da;

 

    [WebMethod]

    public DataSet GetOrderFromdatBase(int OrderId)

    {

        con = new SqlConnection("Data Source=localhost; Initial Catalog=Northwind; Uid=sa; pwd=");

        cmd.CommandText = "Select * from Orders where OrderId=" + OrderId.ToString();

        DataSet ds = new DataSet("OrderSet");

        cmd.Connection = con;

        con.Open();

        da = new SqlDataAdapter(cmd);

        da.Fill(ds, "Orders");

        return ds;       

    }

}

Testing Your Web Service

If I run the application then the window will look like this:



Figure 4: Initially running the Web Service  Service.asmx.

If we click on the link for the GetOrderFromDataBase method, we see the screen like as in figure 5. This browser screen enables us to test the method by entering an OrderID.



Figure 5: Testing the Web Srvice's GetOrderFromDataBase method.

After we click the Invoke button, the browser returns the XML data representing the DataSet, which contains the order ID of 10248. Following figure shows a  piece of the XML data representing the order and displayed by the browser.




Figure 6: The Order DataSet dispalyed by the browser in XML format.

 


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.