An Overview Of Web Services In .NET

In this article, we will discuss the following topics,
  • Introduction to Web Services
  • Components of Web Services
  • SOAP
  • UDDI
  • WSDL
  • Basic Architecture of Web Service
  • Create ASP.NET Web Service Project
  • Consume Web Service in ASP.NET Web Application
  • Include Web Service Reference in Web Application
  • [WebMethod]
  • BufferResponse
  • CacheDuration
  • Description
  • EnableSession
  • MessageName

Web Services

 
A web service is an XML-based information exchange system that creates direct interaction between the two applications over the internet or network in order to exchange data or information. It enables us to communicate and exchange data or information between the two different applications created in the same or different languages over the internet or network. For example, if we use multiple software systems and we need to transfer data from one application to another application then a web service is the best source to accomplish this task. The software system that sends requests for data is called a service requester whereas the software system that would process the request and provide the data is called a service provider. A web service works between all types of applications software and it does not matter if both the service requester software and service provider software are written in different programming languages because web services use XML files for data exchange and most software applications, however, interpret XML tags.
 
Web service accomplishes the application's interactions and data communication among various applications by using a combination of open protocols and standards such as XML, SOAP, WSDL, and UDDI. The XML is an XML file used to tag the data, the SOAP stands for Simple Object Access Protocol. The SOAP is an XML based protocol that is used for sending and receiving messages between the applications without confronting interoperability issues, the WSDL stands for Web Services Description Language and it is used for describing the services available and UDDI stands for Universal Description Discovery and Integration. The UDDI is an XML-based standard that lists what services are available. The web service file extension is ASMX.
 
Components of Web Services
 
There are the following three major components of a Web Service,
  • SOAP
  • UDDI
  • WSDL

SOAP

 
SOAP stands for Simple Object Access Protocol. It is a communication protocol that is responsible for allowing communication and messages transfer between the two different applications of the same or different platform and the same or different programming languages. The SOAP protocol is language independent and platform independent that sends and receives messages between the different applications without facing the interoperability issues. The SOAP communication is carried out through HTTP. A SOAP message is an ordinary XML document that contains the following elements,
  • <Envelope> Element
  • <Header> Element
  • <Body> Element
  • <Fault> Element
The <Envelope> element is the root element of the SOAP message that defines the XML document as a SOAP message. It defines the start and end of the message. It actually defines the SOAP message structure. The <Envelope> element is the mandatory element of the SOAP message. The <Header> element is an optional element of the SOAP message that contains the header information. The header may contain the routing data which is basically the information which tells the XML document to which client it needs to be sent to. The <Header> element is defined inside the <Envelope> element before the <Body> element. The <Body> element is the main element of the SOAP message that contains the actual message. The <Body> element is defined inside the <Envelope> element after the <Header> element. The <Fault> element is an optional element that contains information about errors that occurs while processing the message. The <Fault> element is defined inside the <Body> element. Following is the general skeleton of a SOAP message,
  1. <?xml version="1.0"?>  
  2. <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">  
  3.     <soap:Header> ... </soap:Header>  
  4.     <soap:Body> ... <soap:Fault>  
  5.             <-- The Fault element is an optional element and it is used only if a fault occurs in web service. -->  
  6.         </soap:Fault>  
  7.     </soap:Body>  
  8. </soap:Envelope>  

UDDI

 
UDDI stands for Universal Description Discovery and Integration. The UDDI is an XML-based standard that lists what services are available. The UDDI uses WSDL to describe interfaces to web services. It can communicate via SOAP, CORBA, and Java RMI Protocol.
 

WSDL

 
WSDL stands for Web Service Description Language. It is a standard that describes the availability of service. It enables a web service to tell to the clients that what messages it accepts and which results it will return. The WSDL is written in XML and it is used in the combination with the SOAP and XML schema to provide web services over the internet.
 
Basic Architecture of Web Service
 
A Web Service is a web application that follows code-behind architecture such as the ASP.NET web pages but it does not have a user interface. A Web Service application is basically a class containing a method that is exposed over the Web using simple messaging protocol stacks. The methods of a Web Service are called web methods. Each web method is represented by the [WebMethod] attribute. The Web Service methods are similar to the ordinary methods but the main difference is that a web service method can be accessed by the way of a web browser. A Web Service application is used by other applications called clients applications. To consume a Web Service application, a separate client application is created in any programming language. A client application invokes a Web Service application over HTTP using a web method. When we create a Web Service application using ASP.NET, the following files are automatically created:
 
File Description
.asmx The .ASMX file is the ASP.NET Web Service source file that stands for Active Server Method file. It is similar to .ASPX files but the main difference is that the .ASMX file does not have a graphical user interface and it is the endpoint for ASP.NET Web Services. The .ASMX file moves data and performs other actions behind the scenes. It displays the Web Service methods in a list and allows the user to invoke the Web Service methods. The .ASMX file placed the required web services directive at the top of this file to create an association between the URL address of the Web Service and its implementation. The .ASMX file is used to add Web Services logic to the methods visible by the client application and it acts as the base URL for clients calling the XML web service.
code-behind When we create an ASP.NET Web Service application, a code-behind file is generated with a language-specific extension. For example, if the Visual C# language is used for the ASP.NET Web Service project then a code-behind file is created with the .cs extension and if the Visual Basic language is used for the ASP.NET Web Server project then the code-behind the file is created with the .vb extension. The .cs or .vb file is the code-behind file that contains the Web Service logic. This file contains a class which is inherited from the System.Web.Services.WebService class. The Web Service methods are defined in the class of the .cs or .vb file. Each method of the Web Service is represented by [WebMethod] attribute. The [WebMethod] attribute is placed prior to the method name. The Web Service method is used to define the Web Service logic or a task
.wsdl This file is generated when we add a Web Service application to a client application. This file describes the Web Services interface available to the client.
 
Create ASP.NET Web Service Project
 
To create a Web Service application, first, create a Website then add a Web Service file to the Website. A Website can be created using Microsoft Visual Studio or Microsoft Visual Web Developer. To create a Website using Microsoft Visual Studio.NET, a new project of ASP.NET Website can be created using Microsoft Visual Studio.NET 2015 by using the following steps,
  • Open Microsoft Visual Studio.NET.
  • From the File menu of Microsoft Visual Studio.NET, select New then select Web Site. It will display New Web Site dialog box.
  • The New Web Site dialog box contains three pans that are left side pane, middle pane, and right side pane. From the left side pane, under the Installed Templates, select your desired programming language such as C# or Visual Basic then from the middle pane select ASP.NET Empty Web Site.
  • In the Web location box, select File System; then enter the name of the folder where you want to keep your website.
  • Now, click on the OK button of the new website dialog box. After these steps, an empty website project will be opened.
  • Now add a Web Service file to the website project. To add a Web Service file to the website, right click on the Project Name from the Solution Explorer and select Add then select the Add New Item option. When you select the Add New Item option, it opens the Add New Item window.
  • The Add New Item window contains three panes, these are left side pane, middle pane, and right side pane. From the middle pane of the Add New Item window, select the Web Service file and under the name textbox enter the name for the Web Service file and then press the Add button of the Add New Item window. The name of the Web Service file should be any valid name. The extension of the Web Service file is .ASMX. When the Web Service file is added to the website project then two files are automatically added to the website project. The extension of one of the file is .cs and the extension of the second file is .ASMX. Suppose the name of the Web Service file is WebServiceCalculation then two files are automatically added to the website project. The name of the first file is WebServiceCalculation.cs and the name of the second file is WebServiceCalculation.asmx. These two files are created automatically in the App_Code folder.
Following is the default code of the WebServiceCalculation.cs file,
 
ASP.NET C# Web Service
  1. using System.Web.Services;  
  2. [WebService(Namespace = "http://tempuri.org/")]  
  3. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  4. public class WebServiceCalculation: WebService {  
  5.     public WebServiceCalculation() {  
  6.             //Uncomment the following line if using designed components  
  7.             //InitializeComponent();  
  8.         }  
  9.         [WebMethod]  
  10.     public string HelloWorld() {  
  11.         return "Hello World";  
  12.     }  
  13. }  
The default Web Service contains a single method and when it is invoked using the Web Browser, it displays a string message, Hello World. We can change the code of this method according to our task or functionality and we can also create more Web Service methods according to the number of tasks or functions. For example, if we want to create a Web Service for calculation purposes to add and subtract two integer values then we will create two Web Service methods, one method to perform addition and another method to perform subtraction. To create a Web Service that performs addition and subtraction of two integer values, the above code of the WebServiceCalculation.cs file will be replaced as follows,
 
ASP.NET C# Web Service
  1. using System.Web.Services;  
  2. [WebService(Namespace = "http://tempuri.org/")]  
  3. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  4. public class WebServiceCalculation: WebService {  
  5.     public WebServiceCalculation() {  
  6.             //Uncomment the following line if using designed components  
  7.             //InitializeComponent();  
  8.         }  
  9.         [WebMethod]  
  10.     public int AddTwoValues(int x, int y) {  
  11.             return x + y;  
  12.         }  
  13.         [WebMethod]  
  14.     public int SubtractTwoValues(int x, int y) {  
  15.         return x - y;  
  16.     }  
  17. }  
The above Web Service application contains two web service methods in which one method calculate the sum of two integer values and second method calculate subtraction of the two integer values. Now the Web Service Server application is ready to use. When the above Web Service application is run, the .ASMX file lists the name of the two Web Service methods in the Web Browser. To use a Web Service, a client application is created and it is reference with the Web Service application using the Add Web reference to the website.
 
Consume Web Service in ASP.NET Web Application
 
To consume a Web Service application in ASP.NET Web Application, follow the following steps:
  1. Open a new empty project of ASP.NET Web Application.
     
  2. Now, go to the Solution Explorer, right click on the project name and press the Add option then select Service Reference. The Service Reference opens the Add Service Reference window.
     
  3. Now, from the Add Service Reference window, press the Advanced button. The Advanced button of the Add Service Reference window opens the Service Reference Settings window.
     
  4. Now, from the Service Reference Settings window, press the Add Web Reference button. The Add Web Reference button opens the Add Web Reference window that contains a URL textbox. Now, copy the URL of the .ASMX file of the Web Service application and put this URL in the URL text box of the Add Web Reference window and press the Go button.
     
  5. When we press the Go button, the Web Service is displayed in the browser of the Add Web Reference window and the Web Service file .ASMX is displayed in the browser of the Add Web Reference window then in the Web reference name text box, a default name is displayed for the Web Service that is usually the name of the localhost. We can give any name to the Web reference name.
     
  6. Change the Web reference name from the localhost to any other name, suppose ReferenceWebService, and then press the Add Reference button. The Add Reference button creates a Proxy at the client side. The Web reference name is used as a directive in the client application and it must be included in the client application. We can also use a fully qualified name of the Web Service application main class. The Web Service reference automatically added to the Web Application under the Web references folder.
     
  7. Now add a new Web Form to the Web application and give a meaningful name to it. Suppose the name of the new Web Form is ConsumeWebServiceForm. Now create three textboxes controls and two command button controls on the ConsumeWebServiceForm. The first two textboxes control will read two integer values from the user and the third textbox control is used to display the result of the Web Service method. The first button control is used to send values of the first two textbox control to the AddTwoValues Web Service method and the second button control is used to send the values of the first two textbox controls to the SubtractTwoValues Web Service method.
Include Web Service Reference in Web Application
 
When the Web Service reference is added to the ASP.NET Web Application, the Web reference name is included in it in the directive section of the Web application. When the Web reference name is included in the ASP.NET Web Application, the Web Service class is visible to use its member methods. The member methods of the Web Service class are Web Service methods. To use Web Service methods, in the client application of ASP.NET Web application, an object of the Web Service class is declared and it is used to invoke the Web Service methods. Following is the general syntax to include the Web reference name in the ASP.NET Web application:
 
using ASP.NET Web Application Name.Web reference name;
 
Now, open the Web Form ConsumeWebServiceForm.aspx of the ConsumeWebService Web application in design view and create three textboxes controls and two button controls or put the following code,
 
The design code of the Web Form ConsumeWebServiceForm.aspx,
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2.   
  3. <head runat="server">  
  4.     <title></title>  
  5. </head>  
  6.   
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.         <div>  
  10.             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  11.             <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
  12.             <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  
  13.             <asp:Button ID="Button1" runat="server" Text="Add Values" OnClick="Button1_Click" />  
  14.             <asp:Button ID="Button2" runat="server" Text="Subtract Values" OnClick="Button2_Click" />  
  15.         </div>  
  16.     </form>  
  17. </body>  
  18.   
  19. </html>  
Now, open the code behind file ConsumeWebServiceForm.aspx.cs of the Web Form ConsumeWebServiceForm.aspx and put the following code,
 
Code of the code behind file ConsumeWebServiceForm.aspx.cs of the ConsumeWebService Web application.
  1. using System;  
  2. using ConsumeWebService.ReferenceWebService;  
  3. namespace ConsumeWebService {  
  4.     public partial class ConsumeWebServiceForm: System.Web.UI.Page {  
  5.         //Declare an object from the class of Web Service  
  6.         WebServiceCalculation obj = new WebServiceCalculation();  
  7.         protected void Page_Load(object sender, EventArgs e) {}  
  8.         protected void Button1_Click(object sender, EventArgs e) {  
  9.             int x, y, Result;  
  10.             x = Convert.ToInt32(TextBox1.Text);  
  11.             y = Convert.ToInt32(TextBox2.Text);  
  12.             Result = obj.AddTwoValues(x, y);  
  13.             TextBox3.Text = Result.ToString();  
  14.         }  
  15.         protected void Button2_Click(object sender, EventArgs e) {  
  16.             int x, y, Result;  
  17.             x = Convert.ToInt32(TextBox1.Text);  
  18.             y = Convert.ToInt32(TextBox2.Text);  
  19.             Result = obj.SubtractTwoValues(x, y);  
  20.             TextBox3.Text = Result.ToString();  
  21.         }  
  22.     }  
  23. }  
[WebMethod]
 
It is an attribute that is used in the Web Service applications to mark a method as an XML web service method. When a method is marked as an XML web service method then that method can be exposed in the client application. We cannot access any method of a web service application in a client application without the [WebMethod] attribute. The [WebMethod] attribute is usually used to a Public method of the web service application. We can also use the properties of this attribute to further configure the behavior of the XML Web service method. The ASP.NET makes it possible to map traditional methods to Web Service operations through the [WebMethod] attribute. The [WebMethod] supports a number of properties that control the behavior of the methods. The fully qualified name of the [WebMethod] is System.Web.Services.WebMethod. The [WebMethod] attribute provides the following properties:
  • BufferResponse
  • CacheDuration
  • Description
  • EnableSession
  • MessageName
BufferResponse
 
This property of the WebMethod attribute enables buffering of responses for an XML Web service method. It takes a Boolean value either true or false. When we set it to true then ASP.NET buffers the entire response before sending it down to the client. The buffering is very efficient and helps improve performance by minimizing communication between the worker process and the IIS process. When we set it to false then ASP.NET buffers the response in chunks of 16KB. If we do not want the entire contents of the response in memory at once when we set this property to false. The default value of this property is true.
 
CacheDuration
 
ASP.NET has built-in support for caching the data on the server. Web Services can use the caching support of ASP.NET to cache the result of a web method. The CacheDuration property takes an integer value that indicates the number of seconds that the web method response will remain in the cache. The default value is 0, which means the server doesn't cache the response. When we enable caching, the server holds responses in memory for the cache duration, so we must use caution if we expect responses to be large or if we expect requests to vary widely. Following is the general syntax of the CacheDuration property:
  1. [WebMethod(CacheDuration = 20)]  
In the above declaration, we assign 20 to CacheDuration that means the server will hold the response in memory for 20 seconds.
 
Description
 
This property is used to specify a description for the web service method. Following is the general syntax of Description:
  1. [WebMehtod(Description = “Description”)]  
EnableSession
 
This property of the WebMethod attribute enables session state for an XML Web service method. It takes a Boolean value either true or false. When we set its value to true, the XML Web service can access the session state collection directly from HttpContext.Current.Session.
 
MessageName
 
This property of the WebMethod attribute enables the XML Web service to uniquely identify overloaded methods using an alias.