Overview Of Web Services In .NET

 

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 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 element is the root element of the Cleaner communication that defines the XML document as a Cleaner communication. It defines the launch and end of the communication. It actually defines the Cleaner communication structure. This element is the obligatory element of Cleaner communication. The < Title > element is a voluntary element of the Cleaner communication that contains the title information. The title may contain the routing data which is principally the information that tells the XML document to which customer it needs to be transferred. The < Title > element is defined inside the element before the < Body > element. The < Body > element is the main element of the Cleaner communication that contains factual communication. The < Body > element is defined inside the element after the < Title > element. The element is a voluntary element that contains information about crimes that do while recycling the communication. The element is defined inside the < Body > element. Following is the general shell of a Cleaner communication,

.,

  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 what messages it accepts and which results in 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 operation that follows a law-behind armature similar to the theASP.NET web runners but it doesn’t have a stoner interface. A Web Service operation is principally a class containing a system that’s exposed over the Web using simple messaging protocol heaps. The styles of a Web Service are called web styles. Each web system is represented by the (WebMethod) trait. The Web Service styles are analogous to the ordinary styles but the main difference is that a web service system can be penetrated by the way of a web cybersurfer. A Web Service operation is used by other operations called guests operations. To consume a Web Service operation, a separate customer operation is created in any programming language. A customer operation invokes a Web Service operation over HTTP using a web system. When we produce a Web Service operation usingASP.NET, the following lines are automatically created

.

FileDescription.asmxThe.ASMX train is the theASP.NET Web Service source train that stands for Active GarƧon System train. It’s analogous to. ASPX lines but the main difference is that the. ASMX train doesn’t have a graphical stoner interface and it’s the endpoint forASP.NET Web Services. The. ASMX train moves data and performs other conduct behind the scenes. It displays the Web Service styles in a list and allows the stoner to bring the Web Service styles. The. ASMX train placed the needed web services directive at the top of this train to produce an association between the URL address of the Web Service and its perpetration. The. ASMX train is used to add Web Services sense to the styles visible by the customer operation and it acts as the base URL for guests calling the XML web service. code-behindWhen we produce an anASP.NET Web Service operation, a law-behind train is generated with a language-specific extension. For illustration, if the VisualC# language is used for the theASP.NET Web Service design also a law-behind train is created with the. cs extension and if the Visual Basic language is used for the theASP.NET Web GarƧon design also the law-behind the train is created with the. vb extension. The. cs or. vb train is the law-behind train that contains the Web Service sense. This train contains a class that is inherited from the system.Web.Services.WebService class. The Web Service styles are defined in the class of the. cs or. vb train. Each system of the Web Service is represented by (WebMethod) trait. The (WebMethod) trait is placed previous to the system name. The Web Service system is used to define the Web Service sense or a task .wsdlThis train is generated when we add a Web Service operation to a customer operation. This train describes the Web Services affiliate available to the customer.

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. namespace WebServiceCalculation
  2. {
  3. /// <summary>
  4. /// Summary description for WebService1
  5. /// </summary>
  6. [WebService(Namespace = “http://tempuri.org/")]
  7. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  8. [System.ComponentModel.ToolboxItem(false)]
  9. public class WebService1 : System.Web.Services.WebService
  10. {
  11. [WebMethod]
  12. public string HelloWorld()
  13. {
  14. return “Hello World”;
  15. }
  16. }
  17. }

The dereliction Web Service contains a single system and when it’s invoked using the Web Cybersurfer, it displays a strong communication, Hello World. We can change the law of this system according to our task or functionality and we can also produce further Web Service styles according to the number of tasks or functions. For illustration, if we want to produce a Web Service for computation purposes to add and abate two integer values also we will produce two Web Service styles, one system to perform addition and another system to perform deduction. To produce a Web Service that performs addition and deduction of two integer values, the below the law of theWebServiceCalculation.cs train will be replaced as follows,

.

ASP.NET C# Web Service

  1. namespace WebServiceCalculation
  2. {
  3. /// <summary>
  4. /// Summary description for WebService1
  5. /// </summary>
  6. [WebService(Namespace = “http://tempuri.org/")]
  7. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  8. [System.ComponentModel.ToolboxItem(false)]
  9. public class WebService1 : System.Web.Services.WebService
  10. {
  11. [WebMethod]
  12. public int AddTwoValues(int x, int y)
  13. {
  14. return x + y;
  15. }
  16. [WebMethod]
  17. public int SubtractTwoValues(int x, int y)
  18. {
  19. return x — y;
  20. }
  21. }
  22. }

The below Web Service operation contains two web service styles in which bone system calculate the sum of two integer values and the alternate system calculate the deduction of the two integer values. Now the Web Service GarƧon operation is ready to use. When the below Web Service operation is run, the. ASMX train lists the name of the two Web Service styles in the Web Cybersurfer. To use a Web Service, a customer operation is created and it’s reference with the Web Service operation 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:

Open a new empty project of ASP.NET Web Application.

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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. <head runat=”server”>
  3. <title></title>
  4. </head>
  5. <body>
  6. <form id=”form1" runat=”server”>
  7. <div>
  8. <asp:TextBox ID=”TextBox1" runat=”server”></asp:TextBox>
  9. <asp:TextBox ID=”TextBox2" runat=”server”></asp:TextBox>
  10. <asp:TextBox ID=”TextBox3" runat=”server”></asp:TextBox>
  11. <asp:Button ID=”Button1" runat=”server” Text=”Add Values” OnClick=”Button1_Click” />
  12. <asp:Button ID=”Button2" runat=”server” Text=”Subtract Values” OnClick=”Button2_Click” />
  13. </div>
  14. </form>
  15. </body>
  16. </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’s a trait that’s used in Web Service operations to mark a system as an XML web service system. When a system is marked as an XML web service system also that system can be exposed in the customer operation. We can not pierce any system of a web service operation in a customer operation without the (WebMethod) trait. The (WebMethod) trait is generally used in a Public system of the web service operation. We can also use the parcels of this trait to further configure the geste of the XML Web service system. TheASP.NET makes it possible to collude traditional styles to Web Service operations through the (WebMethod) trait. The (WebMethod) supports a number of parcels that control the geste of the styles. The completely good name of the (WebMethod) system.Web.Services.WebMethod. The (WebMethod) trait provides the following parcels

  • 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 which 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.

Post a Comment

0 Comments