WCF Interview Questions

Q1. What is WCF?

WCF stands for Windows Communication Foundation. It is a Software development kit for developing services on Windows. WCF is introduced in .NET 3.0. in the System.ServiceModel namespace. WCF is based on basic concepts of Service oriented architecture (SOA)

Q2. What is endpoint in WCF service?

The endpoint is an Interface which defines how a client will communicate with the service. It consists of three main points: Address,Binding and Contract.
Every service must have Address that defines where the service  resides, Contract that defines
what  the service  does and a Binding that defines how to communicate with the service. In WCF the relationship between Address, Contract and Binding is called Endpoint.

Q3. Explain Address,Binding and contract for a WCF Service?

Address: Address defines where the service resides.
Binding: Binding defines how to communicate with the service.
Contract: Contract defines what is done by the service.

Q4. Please explain different modes of security in WCF? Or Explain the difference between Transport and Message Level Security.

a.      Transport Level security       b. Message Level Security 
For Tranport level security, we actually ensure the transport that is being used should be secured.
  <binding name="netTcpTransportBinding">
    <security mode=Transport>
      <Transport clientCredentialType=Windows />

Message level security, we actually secure the message. We encrypt the message before transporting it.

  <binding name="wsHttpMessageBinding">
    <security mode=Message>
      <Message clientCredentialType=UserName />

It totally depends upon the requirements but we can use a mixed security mode also as follows:
  <binding name="basicHttp">
    <security mode=TransportWithMessageCredential>
      <Transport />
      <Message clientCredentialType=UserName />

Q5. What is the difference between WCF and Web services?
Web services can only be invoked by HTTP. While Service or a WCF component can be invoked by any protocol and any transport type.

Second web services are not flexible. However, Services are flexible. If you make a new version of the service then you need to just expose a new end.
WCF is flexible because its services can be hosted in different types of applications.

Q6. What are the main components of WCF?
The main components of WCF are
1. Service class
2. Hosting environment
3. End point

Q7. Where we can host WCF services?
Every WCF services must be hosted somewhere. There are three ways of hosting WCF services.
They are
1. IIS
2. Self Hosting
3. WAS (Windows Activation Service)

Q8. What is binding and how many types of bindings are there in WCF?
A binding defines how an endpoint communicates to the world. A binding defines the transport (such as HTTP or TCP) and the encoding being used (such as text or binary). WCF
 supports nine types of bindings.( Basic binding, TCP binding, Peer network binding, IPC binding, Web Service (WS) binding, Federated WS binding, Duplex WS binding, MSMQ binding, MSMQ integration binding).

Q9. What are the types of contract available in WCF?
In WCF, all services expose contracts. The contract is a platform-neutral and standard way of describing what the service does.
The main contracts are:
a)Service Contract: Describes what operations the client can perform.
b)Operation Contract :  defines the method inside Interface of Service.
c)Data Contract: Defines what data types are
passed to and from the service
d)Message Contract: Defines wheather a service can interact directly  with messages

e)Fault Contract: Fault Contract provides documented view for error occurred in the service to client.
 to Read More about Contracts Follow the Link: Read about Contracts

Q10. What is the proxy for WCF Service?
A proxy is a class by which a service client can Interact with the service.
By the use of proxy in the client application we are able to call the different methods exposed by the service

Q11. How to test the WCF Service?
We can use the WCF Test client to test the WCF Service. Tool enables users to input test parameters, submit that input to the service, and view the response.
We can use the following command at command line. 
wcfTestClient.exe URI1 URI2 …

Q12.  What is IIS?
IIS Manager is a graphical interface for configuring your application pools or your Web, FTP, SMTP, or NNTP sites. With IIS Manager, you can configure IIS security, performance, and reliability features. You can add or delete sites; start, stop, and pause sites.

Q12.  Difference between API and WebService.
An API is an Application Programming Interface. An example of an API would be ADO.NET or Google APIs . An API encapsulates functionality into a library for you to access. It contains the operations, properties, etc. necessary to perform your actions.

A web service is a middle tier operation that you write on your own or consume someone Else's. A web service contains functionality that does some sort of operation. 

Q12.  What is SOAP?

Simple Object Access Protocol is a XML based protocol. It enables applications to communicate with each other.

  • SOAP stands for Simple Object Access Protocol
  • SOAP is an application communication protocol
  • SOAP is a format for sending and receiving messages
  • SOAP is platform independent
  • SOAP is based on XML
  • SOAP sits on top of HTTP
The main purpose of using SOAP is to enable communication between two independent applications using different technology, hosted on different environment.

What is the difference between the SOAP and HTTP protocol. When we say "SOAP over HTTP", what does that mean.?
SOAP is messaging protocol concerned with packaging of messages in XML protocol and transfer then using HTTP . 
You can serve any content over HTTP such as HTML, images, sound, video, etc. SOAP is an XML-based encoding of messages that are typically sent over HTTP.

Q13. What is the protocol on which secure pages are generally requested?
HTTPS, "Hyper Text Transfer Protocol" with Secure Sockets Layer (SSL)

Q13. What is Secure Sockets Layer (SSL)?
The Secure Sockets Layer (SSL) ensures that the data sent over the Internet can’t be read by others. When a user requests a secure Web page, the server generates an encryption key for the user’s session and then encrypts the page’s data before sending a response. On the client side, the browser uses that same encryption key to decrypt the requested Web page and to encrypt new requests sent from that page.

Q14. Difference between WCF and Web service
Web service is a part of WCF.  WCF offers much more flexibility and portability to develop a service when comparing to web service. Still we are having more advantages over Web service, following table provides detailed difference between them.
Web Service
It can be hosted in IIS
It can be hosted in IIS, windows activation service, Self-hosting, Windows service
[WebService] attribute has to be added to the class
[ServiceContraact] attribute has to be added to the class
[WebMethod] attribute represents the method exposed to client
[OperationContract] attribute represents the method exposed to client
One-way, Request- Response are the different operations supported in web service
One-Way, Request-Response, Duplex are different type of operations supported in WCF
System.Xml.serialization name space is used for serialization
System.Runtime.Serialization namespace is used for serialization
XML 1.0, MTOM(Message Transmission Optimization Mechanism), DIME, Custom
XML 1.0, MTOM, Binary, Custom
Can be accessed through HTTP, TCP, Custom
Can be accessed through HTTP, TCP, Named pipes, MSMQ,P2P, Custom
Security, Reliable messaging, Transactions

Q15. Serialization is the process of converting an object instance to a portable and transferable format. So, whenever we are talking about web services, serialization is very important.

Serialization is the process of taking an object and converting it to a format in which it can be transported across a network or persisted to a storage location. The storage location could be as simple as using a file or a database. The advantage of serialization is the ability to transmit data across the network in a cross-platform-compatible format.

There are three formats of serialization
• Binary Serialization 
• SOAP Serialization 
• XML Serialization 

The format is controlled based upon what object is used to perform the serialization. The XML format is produced by using the System.Xml.Serialization.XmlSerializer class. The SOAP and binary formats are produced by using classes under the System.Runtime.Serialization.Formatters namespace.

Q16. What is WCF REST Service ?
If the WCF service is using REST/POX, then it is not necessary for the client to consume the WCF service proxy. For REST, WCF introduced a new binding i.e. WebHttpBinding. A client application that can do HTTP communication and can process XML, could now directly make a call to the WCF service and perform operations using XML.

Q17.  How to define a Service as REST based service in WCF?
 WCF 3.5 provides explicit support for RESTful communication using a new binding named WebHttpBinding. 
The below code shows how to expose a RESTful service 
interface IStock
   int GetStock(string StockId);
By adding the WebGetAttribute, we can define a service as REST based service that can be accessible using HTTP GET operation.

Q18. How to set the timeout property for the WCF Service client call?
The timeout property can be set for the WCF Service client call using binding tag. 

      binding = "wsHttpBinding"
      bindingConfiguration = "LongTimeout" 
      <binding name = "LongTimeout" sendTimeout = "00:04:00"/> 
If no timeout has been specified, the default is considered as 1 minute.

Q19. What is the use of ServiceBehavior attribute in WCF ?

ServiceBehaviour attribute is used to specify the InstanceContextMode for the WCF Service class (This can be used to maintained a state of the service or a client too) 

There are three instance Context Mode in the WFC ::
PerSession : This is used to create a new instance for a service and the same instance is used for all method for a particular client. (eg: State can be maintained per session by declaring a variable) 
PerCall : This is used to create a new instance for every call from the client whether same client or different. (eg: No state can be maintained as every time a new instance of the service is created) 
Single : This is used to create only one instance of the service and the same instance is used for all the client request. (eg: Global state can be maintained but this will be applicable for all clients)

Q20. What is the Messaging Pattern? Which Messaging Pattern WCF supports?

Messaging Pattern : Messaging patterns describes how client and server should exchange the message. There is a protocol between client and server for sending and receiving the message. These are also called Message Exchange Pattern. 
WCF supports following 3 types of Message Exchange Patterns 
1. request - reply (default message exchange pattern) 
2. OneWay (Simplex / datagram) 
3. Duplex(CallBack) 

Q21. What is .svc file in WCF?
.svc file is a text file. This file is similar to our .asmx file in web services. 
This file contains the details required for WCF service to run it successfully. 

This file contains following details : 
1. Language (C# / VB) 
2. Name of the service 
3. Where the service code resides 

Example of .svc file 
<%@ ServiceHost Language="C#/VB" Debug="true/false" CodeBehind="Service code files path" Service="ServiceName" >
We can also write our service code inside but this is not the best practice.

Q22. What is DataContractSerializer in WCF?

DataContractSerializer is new WCF serializer. 
This is serialization engine in WCF. DataContractSerializer translate the .NET framework objects into XML and vice-versa. 
By default WCF uses DataContractSeriazer.

Q23. What is the purpose of base address in WCF service? How it is specified?

When multiple endpoints are associated with WCF service, base address (one primary address) is assigned to the service, and relative addresses are assigned to each endpoint. Base address is specified in <host> element for each service. 
<service name=”MyService>
 <add baseAddress =”http://localhost:6070/MyService”>

Q24. Which protocol is used for platform-independent communication?

SOAP (Simple Object Access Protocol), which is directly supported from WCF (Windows Communication Foundation).

Q25. What are tha advantages of hosting WCF service in WAS?

WAS (Windows Activation Service) is a component of IIS 7.0. Following are few advantages : 

1. We are not only limited to HTTP protocol. We can also use supported protocols like TCP, named pipes and MSMQ 
2. No need to completely install IIS. We can only install WAS component and keep away the WebServer. 

Q26.  What is service host factory in WCF?

1. Service host factory is the mechanism by which we can create the instances of service host dynamically as the request comes in. 
2. This is useful when we need to implement the event handlers for opening and closing the service. 
3. WCF provides ServiceFactory class for this purpose. 

Q27. What are Contracts in WCF?

A Contract is basically an agreement between the two parties i.e. Service and Client. In WCF, Contracts can be categorized as behavioral or structural.

Behavioral Contracts
define that what operations client can perform on a service.
Service Contract attribute is used to mark a type as Service contract that contains operations.
Operation Contract attributes is used to mark the operations that will be exposed.
Fault Contract defines what errors are raised by the service being exposed.

Structural Contracts
Data Contract  attribute define types that will be moved between the parties.
Message Contract attribute define the structure of SOAP message.

Q28. What is WSDL? 
WSDL stands for Web Service Description Language. WSDL is an XML based document that provides technical details about the web service. Some of the useful information in WSDL document are:

method name,
port types,
service end point,
method parameters etc.

1 comment:


ADFS (1) ADO .Net (2) Ajax (1) Angular (1) Angular Js (17) Angular2 (28) ASP .Net (14) Authentication (1) Azure (1) Breeze.js (1) C# (50) CD (1) CI (2) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DevOps (4) DI (4) Dotnet (22) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (8) Lamda (3) Linq (11) Mongodb (1) MVC (50) NodeJS (7) RDLC (1) Report (1) SDLC (1) Sql Server (30) SSIS (3) SSO (1) SSRS (2) UI (1) WCF (13) Web Api (11) Web Service (1) XMl (1)