WCF Question and answers for Interview

What is wcf??

Let's understand WCF is a platform for building distributed businesses and deploying services among various endpoints in Windows. WCF was initially called “Indigo” and we can build service-oriented applications and provide interoperability.

This means WCF or Windows Communication Foundation is a programming model to create service oriented applications. It is used to create and deploy the service that is accessible to lots of different clients. It provides an environment where you can create a service which can be accessible to Windows clients as well as Linux clients or any others. It provides more features compared to web services.

1. What is the Difference between WCF and ASMX Web Services?

It is very easy to understand this Let's :A simple and basic difference is that ASMX or ASP.NET web service is designed to send and receive messages using SOAP over HTTP only. While WCF can exchange messages using any format (SOAP is default) over any transport protocol (HTTP, TCP/IP, MSMQ, NamedPipes, etc).

2. What are WCF Service Endpoints? Explain.

Actually Windows Communication Foundation services to be consumed, it’s necessary that it must be exposed; Clients need information about service to communicate with it. This is where service endpoints play their role.
WCF service endpoint has three basic elements that means piller, i.e. AddressBinding and Contract.
  • Address: It defines "WHERE". Address is the URL that identifies the location of the service.
  • Binding: It defines "HOW". Binding defines how the service can be accessed.
  • Contract: It defines "WHAT". Contract identifies what is exposed by the service.

3: Why Should We Use WCF Service?

Let's see Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Most of WCF functionality is included in a single assembly called System.ServiceModel.dll, located in the System.ServiceModel namespace. 
WCF Architecture

Then Why WCF uses??

  1. A web service to exchange messages in XML format using HTTP protocol for interoperability.
  2. A remoting service to exchange messages in binary format using TCP protocol for performance.
    • A secure service to process business transactions.
    • A service that supplies current data to others, such as a traffic report or other monitoring service.
    • A chat service that allows two people to communicate or exchange data in real time.
    • A dashboard application that polls one or more services for data and presents it in a logical presentation.
    • A Silverlight application to poll a service for the latest data feeds.

4. What are the Possible Ways of Hosting a WCF Service? Explain Please

For a Windows Communication Foundation service to host, we need at least a managed process, a ServiceHost instance and an Endpoint configured. Possible approaches for hosting a service are:
  1. Hosting in a Managed Application/ Self Hosting
    1. Console Application
    2. Windows Application
    3. Windows Service
  2. Hosting on Web Server
    1. IIS 6.0 (ASP.NET Application supports only HTTP)
    2. Windows Process Activation Service (WAS) i.e. IIS 7.0 supports HTTP, TCP, NamedPipes, MSMQ.

5. How Can We do Operation Overloading While Exposing WCF Services?

By default, WSDL doesn’t support operation overloading. Overloading behavior can be achieved by using "Name" property of OperationContract attribute.
[ServiceContract]
interface ICalculator
{
   [OperationContract(Name = "SumInt")]
   int Sum(int arg1,int arg2);

   [OperationContract(Name = "SumDouble")]
   double Sum(double arg1,double arg2);
}

Please Explain Different Instance Modes in WCF?

Actually WCF will bind an incoming message request to a particular service instance, so the available modes are:
  • Per Call: Instance created for each call, most efficient in term of memory but need to maintain session.
  • Per Session: Instance created for a complete session of a user. Session is maintained.
  • Single: Only one instance created for all clients/users and shared among all. Least efficient in terms of memory.

What is Contract??? What are the types of Contract???

Contract is the agreement between client and service which explain following:
  • [ServiceContract] - this specify which services are exposed to the client.
  • [OperationContract] - this specify which operations the client can perform on the service.
  • [DataContract] – this specify which data types are passed to and from the service.
  • [MessageContract] -This  allow the service to interact directly with messages. Message contracts can be typed or untyped and are useful in interoperability cases when another party has alreadydictated some explicit (typically proprietary) message format.
  • [FaultContract] -this specify which errors are raised by the service and how the service handles and propagates errors to its clients.

What is ABC in WCF?

This is mostly asked question by interviewer.Let's understand this
Endpoint = Address (A) + Binding (B) + Contract (C)
Address specifies where the services is hosted.
Binding specifies how to access the hosted service. It specifies the transport, encoding, protocol etc.
Contract specifies what type of data can be sent to or received from the service.
Let's see example:

<endpoint name="BasicHttpAirlineService" address="http://localhost:5487/MyService/AirlineService.svc" binding="basicHttpBinding" contract="MyNamespace.MyService.IAirlineService" />



Post a Comment

1 Comments