Web Services Interview Questions

What is a Web Service?

A web service is a web-based functionality accessed using the protocols of the web to be used by the web applications and uses standard XML Messaging for communication.

XML is used to encode all communications to a Web service. For example, a client invokes a Web service by sending an XML message, then waits for a corresponding XML response. Because all communication is in XML, Web services are not tied to any one operating system or programming.

What is the Serialization used for WebService?

Web Services uses XML Serialization.

What is WSDL?

WSDL is an acronym for Web Services Description Language. It is a format to describe what a Web Service is going to offer, generally Operations, Definition and Service bindings.

What is UDDI?

UDDI is an acronym for Universal Description, Discovery, and Integration. UDDI is used for locating the web service.

What is the Web service protocol stack?

The Web service protocol stack is an evolving set of protocols used to define, discover, and implement Web services. The core protocol stack consists of four layers:

Service Transport: This layer is responsible for transporting messages between applications. Currently, this includes HTTP, SMTP, FTP, and newer protocols, such as Blocks Extensible Exchange Protocol (BEEP).

XML Messaging: This layer is responsible for encoding messages in a common XML format so that messages can be understood at either end. Currently, this includes XML-RPC and SOAP.

Service Description: This layer is responsible for describing the public interface to a specific Web service. Currently, service description is handled via the WSDL.

Service Discovery: This layer is responsible for centralizing services into a common registry, and providing easy publish/find functionality. Currently, service discovery is handled via the UDDI.

Beyond the essentials of XML-RPC, SOAP, WSDL, and UDDI, the Web service protocol stack includes a whole zoo of newer, evolving protocols. These include WSFL (Web Services Flow Language), SOAP-DSIG (SOAP Security Extensions: Digital Signature), and USML (UDDI Search Markup Language).

What is the extension for Web Service?

.asmx

What is the namespace for Web Service?

System.Web.Services

Sample Web Service

Creating Service:

Create a New ASP.NET Application and add a Web Service.

The following example demonstrates a sample web service, which sends information of an Employee. Remember that, we have to use [WebMethod] attribute to expose service method.

  1. using System;  
  2.   
  3. using System.Collections.Generic;  
  4.   
  5. using System.Linq;  
  6.   
  7. using System.Web;  
  8.   
  9. using System.Web.Services;  
  10.   
  11. namespace SampleWebService  
  12.   
  13. {  
  14.   
  15.     /// <summary>  
  16.   
  17.     /// Summary description for Test  
  18.   
  19.     /// </summary>  
  20.   
  21.     [WebService(Namespace = "http://tempuri.org/")]  
  22.   
  23.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  24.   
  25.     [System.ComponentModel.ToolboxItem(false)]  
  26.   
  27.     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.  
  28.   
  29.     // [System.Web.Script.Services.ScriptService]  
  30.   
  31.     public class Employee  
  32.   
  33.     {  
  34.   
  35.         public int EmpID   
  36.       {  
  37.             get;  
  38.             set;  
  39.         }  
  40.   
  41.         public string EmpName   
  42.         {  
  43.             get;  
  44.             set;  
  45.         }  
  46.   
  47.         public string country   
  48.         {  
  49.             get;  
  50.             set;  
  51.         }  
  52.   
  53.     }  
  54.   
  55.     public class Test: System.Web.Services.WebService  
  56.   
  57.     {  
  58.   
  59.         [WebMethod]  
  60.   
  61.         public Employee GetEmployee()  
  62.   
  63.         {  
  64.   
  65.             return new Employee {  
  66.                 EmpID = 1, EmpName = "Sridhar", country = "India"  
  67.             };  
  68.   
  69.         }  
  70.   
  71.     }  
  72.   
  73. }  

Let us browse this service and see how it looks: Just right click on asmx file and click browse.


So we are done with Service, let us consume it in a client.

Consuming Service:

Create a Console Application and right click on References and click Add Service Reference,


This opens a new window, click on Discover to find the Service as shown below and provide Namespace:


Click OK, now we are ready to consume the service. Copy the following code, myTest is the namespace and TestSoapClient is proxy to the service which helps us for communication with service.

  1. using System;  
  2.   
  3. using System.Collections.Generic;  
  4.   
  5. using System.Linq;  
  6.   
  7. using System.Text;  
  8.   
  9. namespace SampleClient  
  10.   
  11. {  
  12.   
  13.     class Program  
  14.   
  15.     {  
  16.   
  17.         static void Main(string[] args)  
  18.   
  19.         {  
  20.   
  21.             //Create Proxy for Web Service  
  22.   
  23.             myTest.TestSoapClient proxyClient = new myTest.TestSoapClient();  
  24.   
  25.             myTest.Employee emp = new myTest.Employee();  
  26.   
  27.             //Calling WebServiceMethod 'GetEmployee' WebMethod  
  28.   
  29.             emp = proxyClient.GetEmployee();  
  30.   
  31.             Console.WriteLine("Emp ID:" + emp.EmpID);  
  32.   
  33.             Console.WriteLine("Emp Name:" + emp.EmpName);  
  34.   
  35.             Console.WriteLine("Country:" + emp.country);  
  36.   
  37.             Console.ReadLine();  
  38.   
  39.         }  
  40.   
  41.     }  
  42.   
  43. }  

Output


Let us see the app.config of console application. WebService uses only basicHttpBinding, that’s the reason Bindings have been mentioned as basicHttpBinding.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2.   
  3.     <configuration>  
  4.   
  5.         <system.serviceModel>  
  6.   
  7.             <bindings>  
  8.   
  9.                 <basicHttpBinding>  
  10.   
  11.                     <binding name="TestSoap" />  
  12.   
  13.                 </basicHttpBinding>  
  14.   
  15.             </bindings>  
  16.   
  17.             <client>  
  18.   
  19.                 <endpoint address="http://localhost:56079/Test.asmx" binding="basicHttpBinding" bindingConfiguration="TestSoap" contract="myTest.TestSoap" name="TestSoap" />  
  20.   
  21.             </client>  
  22.   
  23.         </system.serviceModel>  
  24.   
  25.     </configuration>  


Similar Articles