Getting Started With ASP.NET Web Services - Part One

Web Service

A Web service is a class which is fitted with [WebService] attribute and it inherits from System.Web.Services.WebService base class. This attribute is responsible to tell us that a class contains the code for a Web service.

Web Service is a Web Application to develop inter-operable Applications; that is enabling an Application to invoke a method of another Application. These inter-operable Applications can be used on the same computer or different computers as well. A Web service uses open mechanism, standards and protocols i.e. HTTP, XML and SOAP as these are open and very well-known protocols.

Applications are built on any platform that can be inter-operable with the Web services. .NET application can be inter-operable with a Web service built using a Java Application. Similarly, a Web service built using .NET can be consumed by a Java Application.

HTTP
 
Hypertext Transfer Protocol is the protocol which is widely used to send and receive messages by the Web services in the Applications.

SOAP
 
Simple Object Access Protocol is a messaging protocol. SOAP messages are in XML format.

ASMX (Active Server Method Extended) is an extension of Web services due to which Web services are also often called  ASMX web services. ASMX web services are a Bequest or Legacy technology.

To create a Web service, just flip to Visual Studio.

Go to New and choose project.

 

Select ASP.NET Web Application from menu and give a name to your project, hit “OK” button.
 


I’m ready with my solution i.e WebServiceIntro, right click onto project and add a new item.
 


I need to add an asmx page, give a name to it and click “Add” button.
 


Make some changes in the page, as shown in the screenshot below:
 


Let’s understand the code which was auto generated for us-

public class MyWebService : System.Web.Services.WebService- The web service, which I’ve added is a class. This class has been fitted with [WebService] attribute. MyWebService class is inherited from System.Web.Services.WebService is for our Application and is using different kinds of objects like session object, Application objects.

[WebService] attribute tells me that my class is containing a code for our web service.

Web Service namespace is responsible for uniquely identifying our Web service on the Internet from other services that already reside on the Web. The namespace can be any string, but it is common to give your internet domain name or it would be the company’s internet domain name. Hence, the Web service namespace would be like this,

[WebService(Namespace=http://c-sharpcorner.com/webservices)]

This namespace is going to ensure that our Web service is uniquely identified.

[WebService(Namespace = "http://c-sharpcorner.com/webservices")]

[WebMethod] - If you want to expose a method as a part of Web service to a client that it is calling an Application, then the method needs to be fitted with [WebMethod] attribute. If you don’t fit it with [WebMethod] attribute, then the client Application can’t see that method.
If you want to expose your method to the client, then the method needs to be fitted with [WebMethod] attribute and that method needs to be declared public as well. This attribute has several properties like BufferResponse, CacheDuration, Description,EnableSession etc., which are basically used to customize the behaviour of your Web service. These properties are explained in upcoming articles.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Services;  
  6.   
  7. namespace WebServicesIntro  
  8. {  
  9.     /// <summary>  
  10.     /// Summary description for MyWebService  
  11.     /// </summary>  
  12.     [WebService(Namespace = "http://c-sharpcorner.com/webservices")]  
  13.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  14.     [System.ComponentModel.ToolboxItem(false)]  
  15.     // [System.Web.Script.Services.ScriptService]  
  16.     public class MyWebService : System.Web.Services.WebService  
  17.     {  
  18.   
  19.         [WebMethod]  
  20.         public string AddName(string FirstName, string SecondName)  
  21.         {  
  22.             return FirstName + SecondName;  
  23.         }  
  24.   
  25.         [WebMethod]  
  26.         public int Sub(int FirstNumber, int SecondNumber)  
  27.         {  
  28.             return FirstNumber - SecondNumber;  
  29.         }  
  30.     }  
  31. }  
We declare two methods, AddName and Sub, with the return types as string and int with [WebMethod] attribute. I’ve passed two parameters in both the methods.

Right click on to asmx page and set it as start page and view it in the Browser to run it.
 


As shown in the following screenshot, we are invoking MyWebService that we’ve just created. The protocol is http, but you can’t see because I’m using Google Chrome. Look here, I have a link Service Description and if I click on this link-
 


In URL, which is WSDL stands for Web Service Description Language is basically used by the client to generate the proxy classes.
 


Go back to your Web service and you can see there are two methods, click AddName method.
 


Look at the parameters First Name and Second Name, pass value to it. After passing parameter value, click “Invoke” button.
 


We’ll get our result as AmitMishra.
 


Similarly, for the second method that is Sub.
 


Look at the parameters FirstNumber and SecondNumber, pass value to it. After passing parameter value click “Invoke” button.
 


We got our result as 0.
 


To test the Web service method in this page, you also have the sample request and response SOAP Messages, so the following code is in XML format but it is formatted according to the SOAP protocol.

This is the format of the request,
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  
  3.     <soap:Body>  
  4.         <Sub xmlns="http://c-sharpcorner.com/webservices">  
  5.             <FirstNumber>int</FirstNumber>  
  6.             <SecondNumber>int</SecondNumber>  
  7.          </Sub>  
  8.     </soap:Body>  
  9. </soap:Envelope>  
This is the format of the response,
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  
  3.     <soap:Body>  
  4.         <AddNameResponse xmlns="http://c-sharpcorner.com/webservices">  
  5.             <AddNameResult>string</AddNameResult>  
  6.         </AddNameResponse>  
  7.     </soap:Body>  
  8. </soap:Envelope>  
The screenshot of the request and response messages is shown below:
 


Thanks for reading this article. I hope you enjoyed this. For more articles on Web service, stay tuned with us.


Similar Articles