Important Web service interview questions and answers

In this blog, I have collected Web Service interview questions and answers which are very important for interview. Whether you are a fresher or have some development experience, the Web Service interview questions given below will prove to be very beneficial for you.  

1. What is Web Service?

Web Service is the mechanism to communicate 2 or more systems over the web using SOAP and XML.

It is a way to communicate 2 applications developed in any language and available on any platform. i.e. Web service provides interoperability features. A web Service file extension is .ASMX

2. What is SOAP?

SOAP stands for Simple Object Access Protocol. It is an XML based messaging protocol used in Web service to exchange data.

SOAP contains below elements.
An envelope which defines the XML document.
Header – It contains header information.
Body – It contains the response information.
Fault – It contains fault and error information.

3. SOAP vs REST

Differences between REST and SOAP

RESTSOAP
Representational State TransferSimple Object Access Protocol
REST is an architectural styleSOAP is an XML based protocol
REST is lightweight and faster than SOAPSOAP is a traditional protocol but not faster than REST
REST is statelessSOAP is stateful
REST do not have a concept called WSDLSOAP uses WSDL to describe web services

Read more about SOAP and REST.

4. What is SOA?

Service Oriented Architecture (SOA) is a style to design software system where 2 or more component communicates with each other over a network through a defined protocol.

5. Advantages of SOA architecture.

Loosely Couple – A component in SOA architecture must be loosely coupled. One service does not require to know the technical and platform details about another service.

Reusable Component – In SOA all components are are reusable.

Read About SOA – Service Oriented Architecture

6. What is UDDI?

UDDI stands for Universal Description Discovery and Integration. UDDI is a web directory for a service provider who wants to make their services available publicly.

7. What is WSDL?

WSDL stands for Web Service Description Language.
WSDL is an XML document that describes web service. So service provider will communicate service description using SOAP protocol. The consumer will query from the service description using the same protocol.

8. Web services vs .Net Remoting

Both Web service and .Net Remoting was introduced to perform

9. What are Web Service protocol stacks

1. Transport (HTTP)
2. Messaging (XML, SOAP)
3. WSDL
4. UDDI

10. How to enable Session in Web Service?

To enable session in a web method add “EnableSession=true” attribute to the webmethod.

[WebMethod(EnableSession=true)]
        public string HelloWorld(string userName)
        {
            //Write Code here
        }

11. How to do method overloading in Web Service?

If you want to use Method overloading i.e. multiple methods with same name and different parameters, then you must use MessageName attribute to the methods.

If you will not add MessageName attribute to use method overloading, you will get below error when you browse web service.

Both System.String HelloWorld(System.String, System.String) and System.String HelloWorld(System.String) use the message name ‘HelloWorld’.  

Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.

Below sample code to implement method overloading in web service.

[WebMethod(MessageName=“Hello”,Description=“To Say Hello”)]
        public string HelloWorld(string userName)
        {
            Session[“user”] = userName;
            return “Hello” + Session[“user”].ToString();
        }
        [WebMethod(MessageName=“Welcome”, Description=“to say Welcome”)]
        public string HelloWorld(string userName, string ss)
        {
            Session[“user”] = userName;
            return “Welcome” + Session[“user”].ToString();
        }

12. How to call a Web Service using AJAX?

To call a web service using AJAX, you need to use [ScriptService] attribute to your web service class.

ScriptService is a class which indicates that a web service can be invoked from the script.

<div>
        <a href=”javascript:void(0);” id=”testLink”>Click Here</a>
    </div>
    </form>
    <script src=”Scripts/jquery-1.4.1.min.js” type=”text/javascript”></script>
    <script>
        $(‘#testLink’).click(function () {
        var name = ‘Ram’;
            $.ajax({
                url: “http://localhost/WebServiceTest.asmx/HelloWorld”,
                data: ‘{“userName”:”‘ + name + ‘”}’,
                contentType: “application/json; charset=utf-8”,
                type: “POST”,
                success: function (data) {
                    alert(data.d);
                },
                error: function (error) {
                    alert(error.responseText);
                }
            });
        });
    </script>
[ScriptService]
    public class WebServiceTest : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld(string userName)
        {
            return “Hello “ + userName;
        }
    }

13. How to secure a web service?

Below are few authentication options that are available to web service in ASP.NetWindows BasicWindows Basic Over SSLWindows Client CertificatesCustom SOAP HeadersYou can check Custom SOAP Headers mechanism to implement Authentication in my another blog –

How to create a secure Web Service?

14. What are Marshalling and Unmarshalling?

Marshalling is the process of converting XML data into a readable form.
Unmarshalling is just the reverse of Marshalling.

15. How to restrict a Web Service method for HttpGET or HttpPOST?

The default invoke method is POST for a web service method.
If you want to invoke a web service method using GET then you must add an attribute to your web method.

To allow a web method access using GET method, you must use UseHttpGet=true
If you want to access a web method using POST, either you do not use this attribute or UseHttpGet = false.

Default invoke method is POST.

public class WebServiceTest : System.Web.Services.WebService
    {
        [WebMethod]
        [System.Web.Script.Services.ScriptMethod(UseHttpGet=true)]
        public string HelloWorld(string userName)
        {
            return “Hello “ + userName;
        }
    }

Hope you like these Web Service interview questions and answers.

Other Interview Questions and Answers –

Important ASP.Net Interview Questions and Answers
MVC Interview Questions and Answers
Top Web API interview questions and answers
OOPS Interview Questions and Answers
C# Important interview questions and answers

Leave a Comment

RSS
YouTube
YouTube
Instagram