WCF Interview Questions

1) What is WCF?
WCF stands for Windows Communication Foundation. It is a framework which is used for building, configuring and deploying interoperable distributed services. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application. An endpoint can be a client of a service that requests data from a service endpoint. It provides a facility to write more secure, flexible services without any code change. It also provides built-in support for logging. It facilitates you to enable/disable logging using configuration.

Its code name is “Indigo”.

2) What are the essential components used in WCF?
A list of essential components used in WCF:

Service class

The service runtime layer contains the behaviors that occur only during the actual operation of the service, that is, the runtime behaviors of the service. Throttling controls how many messages are processed, which can be varied if the demand for the service grows to a preset limit.

Endpoint

WCF Service is a program that exposes a collection of the endpoints. Each Endpoint is a portal for communicating with the world. All the WCF communications are taken place through the endpoint. An endpoint consists of three components.

Hosting Environment

A service must be hosted in some process. A host is an application that controls the lifetime of the service. Services can be self-hosted or managed by an existing hosting process.

3) What are the WCF service endpoints?
WCF service endpoint has three basic elements: address, binding, and contract.

Address: It defines the address of the URL that identifies the location of the service and specifies the location where messages are received. It is specified as a Uniform Resource Identifier (URI). The URI schema part names the transport mechanism to use to reach the address, such as HTTP and TCP. The hierarchical part of the URI contains a unique location whose format is dependent on the transport mechanism.

The endpoint address enables you to create unique endpoint addresses for each endpoint in service or, under certain conditions, to share an address across endpoints. The following example shows an address using the HTTPS protocol with a non-default port:

https://cohowinery:8005/ServiceModelSamples/CalculatorService
Binding: It defines how the service can be accessed, how an endpoint communicates to the world. It is constructed of a set of components called binding elements that “stack” one on top of the other to create the communication infrastructure. At the very least, a binding defines the transport (such as HTTP or TCP) and the encoding being used (such as text or binary). A binding can contain binding elements that specify details like the security mechanisms used to secure messages or the message pattern used by an endpoint.

Contract: It defines what is exposed by the service. Ties together multiple related operations into a single functional unit. The contract can define service-level settings, such as the namespace of the service, a corresponding callback contract, and other such settings. In most cases, the contract is defined by creating an interface in the programming language of your choice and applying the ServiceContractAttribute attribute to the interface. The actual service code results by implementing the interface.

Operation contract

An operation contract defines the parameters and returns type of operation. When creating an interface that defines the service contract, you signify an operation contract by applying the OperationContractAttribute attribute to each method definition that is part of the contract. The operations can be modeled as taking a single message and returning a single message, or as taking a set of types and returning a type. In the latter case, the system will determine the format for the messages that need to be exchanged for that operation.

Message contract

Describes the format of a message. For example, it declares whether message elements should go in headers versus the body, what level of security should be applied to what elements of the message, and so on.

Fault contract

Can be associated with a service operation to denote errors that can be returned to the caller. An operation can have zero or more faults associated with it. These errors are SOAP faults that are modeled as exceptions in the programming model.

Data contract

The descriptions in metadata of the data type that a service uses. This enables others to interoperate with the service. The data types can be used in any part of a message, for example, as parameters or return types. If the service is using only simple types, there is no need to use data contracts explicitly.

Play

Next
Mute
Current Time
0:00
/
Duration
18:10

Fullscreen

Backward Skip 10s

Play Video

Forward Skip 10s

4) What is the “Address” property of endpoint in WCF?
The “Address” property is the part of endpoint defined in service level. This property is used to specify the location where the service is located.

5) What is the “Binding” property of endpoint in WCF?
The “Binding” property is the part of endpoint defined in service level. This property is used to specify the type protocols, encodings, and transport.

These all factors are decided by both the communicating parties.

6) What is the “Contract” property of endpoint in WCF?
The “Contract” property is just an interface between the client and server where client and server communicate with each other. Contracts are used to specify operations available.

7) What is the service?
A service is a set of functionality exposed to the world. Service orientation (SO) is an abstract set of principles and best practices for building service-oriented applications.

8) What is service proxy in WCF?
WCF Proxies are used to communicate between client and server. The communication takes place by exchanging the messages in the form of requests and responses. It will have the details like Service Path, Protocol details and so on.

9) What is “Service Contracts” in WCF?
The Service Contracts attribute is used at the service level for WCF service. It provides the list of operations that can be performed from that service.

Service Contracts can be defined as:

[ServiceContract]

10) What are the different instance modes in WCF?
A list of instance modes in WCF:

Per Call
Singleton
Per Session

11) What is “Per Session” instance mode in WCF?
Per session instance mode creates a logical session between service and client and it will be maintained till the end of the session. When client requests from service the session will be created and it is dedicated to the instance for that client and it will be going to the end when client session ends.

12) What is “Per Call” instance mode in WCF?
When a request has made to service, it creates a new instance of service for each method call and this will be disposed of once the response goes to the client. This whole process is known as per call instance mode.

13) What is “Singleton” instance mode in WCF?
In “Singleton” mode all the clients are connected to the single instance of the service and when service configured for “Singleton” mode, an instance will be created when service is hosted and it will be disposed of once it shuts down.

14) What do you mean by client?
The client of a service is the program unit consuming its functionality. A client can be anything like a Console application, Windows form, WPF or Silverlight class or ASP.Net page etc.

15) How do WCF works?
WCF follows the model “Software as a Service”. In this model all units of functionality are defined as services and for communication, each point is a portal or connection either with the client or other services. It is a program that exposes a collection of endpoints.

16) What is the difference between ASMX web services and WCF?
The main difference between WCF and ASMX web service is that ASMX is designed to send and receive messages using SOAP over HTTP only while WCF facilitates you to send and receive messages using any format over any transport protocol.

ASMX web services can be hosted only in IIS while WCF service has all the following hosting options:

IIS
WAS (Windows Process Activation Services)
Console Application
Windows NT Services
WCF provided Host
ASMX web services support is limited to HTTP while WCF supports HTTP, TCP, MSMQ, NamedPipes.
ASMX Security is limited. Normally authentication and authorization are done using IIS and ASP.NET security configuration and transport layer security. For message layer security, WSE can be used.
WCF provides a consistent security programming model for any protocol and it supports many of the same capabilities as IIS and WS-* security protocols, additionally, it provides support for claims-based authorization that provides finer-grained control over resources than role-based security. WCF security is consistent regardless of the host that is used to implement WCF service.
Another major difference is that ASMX web services use XmlSerializer for serialization while WCF uses DataContractSerializer which is far better in performance than XmlSerializer.
17) How many types of contract WCF define?
There are four types of contracts that WCF define:

Service Contracts

The ServiceContract attribute maps a CLR interface (or inferred interface, as you will see later on) to a technology-neutral service contract. The ServiceContract attribute exposes a CLR interface (or a class) as a WCF contract, independently of that type’s visibility.

Data Contracts

The descriptions in metadata of the data type that a service uses. This enables others to interoperate with the service. The data types can be used in any part of a message, for example, as parameters or return types. If the service is using only simple types, there is no need to use data contracts explicitly.

Fault Contracts

Can be associated with a service operation to denote errors that can be returned to the caller.

Message Contracts

Describes the format of a message. For example, it declares whether message elements should go in headers versus the body, what level of security should be applied to what elements of the message, and so on.

18) What are the requirements for hosting a WCF service?
You need at least a managed process, a ServiceHost instance and an Endpoint configured for hosting a WCF service.

Following are the possible approaches for hosting a service:

Hosting in a Managed Application/ Self Hosting:
Console Application
Windows Application
Windows Service
Hosting on Web Server
IIS 6.0 (ASP.NET Application supports only HTTP)
Windows Process Activation Service (WAS) i.e. IIS 7.0 supports HTTP, TCP, NamedPipes, MSMQ.

19) Which are the different isolation levels in WCF?
Following is a list of different isolation levels in WCF:

Read Uncommitted: Also known as dirty isolation level. It makes sure that corrupt Data cannot be read. This is the lowest isolation level.
Read Committed: It ensures not to read the data that has been changed by any other application and is not yet committed. It is the default level.
Repeatable Read: It stops the usage of dirt read and non-repeatable read. It states that data fetched through a query will be locked and will not be updated by any other transaction.
Serializable: It does not allow any modification and addition of new data till the transaction is completed. This is considered to be a very restrictive level.
Snapshot: It raises an error on modifying a data that has already been changed by any transaction.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *