Frequently asked Web service interview questions and answers

In this blog, I have collected frequently asked Web service questions in an interview.

What is Web Service?

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

Web service 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

What is SOAP?

SOAP stands for Simple Object Access Protocol. SOAP 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.

SOAP vs REST

Differences between REST and SOAP

REST
SOAP
Representational State Transfer
Simple Object Access Protocol
REST is an architectural style
SOAP is an XML based protocol
REST is lightweight and faster than SOAP
SOAP is a traditional protocol but not faster than REST
REST is stateless
SOAP is stateful
REST do not have a concept called WSDL
SOAP uses WSDL to describe web services

Read more about SOAP and REST.

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.

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

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.

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.

Web services vs .Net Remoting

Both Web service and .Net Remoting was introduced to perform

What are Web Service protocol stacks

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

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
        }


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();
        }



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;
        }
    }


Security in Web Service or How to secure a web service?

Below are few authentication options that are available to web service in ASP.Net

Windows Basic
Windows Basic Over SSL
Windows Client Certificates
Custom SOAP Headers

You can check Custom SOAP Headers mechanism to implement Authentication in my another blog -

How to create a secure Web Service?

What are Marshalling and Unmarshalling?

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

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;
        }
    }



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

Comments

Popular