WCF Interview Questions and Answers

Here is a list of Interview Questions with Answers on WCF. Hope you find this collection useful.

What are the different platforms where we can host WCF service ?

 Where we can host WCF services?

There are number of hosting options available for WCF Services.

You can host WCF services in any of the following Hosting Services.

1.       Self-Hosting in a Managed Application

2.       Managed Windows Services

3.       Internet Information Services (IIS)

4.       Windows Process Activation Service (WAS)

 

 

Resources:

1.       WCF Hosting Services

2.       How to: Host a WCF Service in a Managed Application

3.       How to: Host a WCF Service in a Managed Windows Service

4.       How to: Host a WCF Service in IIS

5.       How to: Host a WCF Service in WAS

 

What are contracts in WCF?

Let us explain what contracts are in WCF.

A WCF Contract is a collection of Operations that specifies what the Endpoint communicates to the outside world. Each operation is a simple message exchange, for example one-way or request/reply message exchange.

The ContractDescription class is used to describe WCF Contracts and their operations. Within a ContractDescription, each Contract operation has a corresponding OperationDescription that describes aspects of the operation such as whether the operation is one-way or request/reply. Each OperationDescription also describes the messages that make up the operation using a collection of MessageDescriptions.

A ContractDescription is usually created from an interface or class that defines the Contract using the WCF programming model. This type is annotated with ServiceContractAttribute and its methods that correspond to Endpoint Operations are annotated with OperationContractAttribute. You can also build a ContractDescription by hand without starting with a CLR type annotated with attributes.

A duplex Contract defines two logical sets of operations: A set that the Service exposes for the Client to call and a set that the Client exposes for the Service to call. The programming model for defining a duplex Contract is to split each set in a separate type (each type must be a class or an interface) and annotate the contract that represents the service's operations with ServiceContractAttribute, referencing the contract that defines the client (or callback) operations. In addition, ContractDescription contains a reference to each of the types thereby grouping them into one duplex Contract.

Similar to Bindings, each Contract has a Name and Namespace that uniquely identify it in the Service's metadata.

Each Contract also has a collection of ContractBehaviors that are modules that modify or extend the contract's behavior

Here is a Code example of how to Define a Contract in WCF

using System.ServiceModel;

 

//WCF contract defined using an interface

[ServiceContract]

public interface ICalculate

{

    [OperationContract]

    int Add(int x, int y);

}

 

 

Here is a Code example of how to Implement a Contract in WCF

//the service class implements the WCF contract defined as an Interface

public class MathService : ICalculate

{

    public int Add(int x, int y)

    { return x + y; }

}

 

 

Different types of WCF Contracts:

 

Service Contract

Service Contract Example

using System.ServiceModel;

 

[ServiceContract]

public interface ICalculate

{

   [OperationContract]

   double Add( double a, double b);

   [OperationContract]

   double Subtract( double a, double b);

}

 

 

Data Contract

 

Data Contract Example

 

[DataContract]

public class Person

{

   [DataMember]

   public int Id;

   [DataMember]

   public string FirstName;

   [DataMember]

   public string LastName;

}

 

 

Resources:

1.       Introduction to Building WCF Services

2.       WCF Architecture Overview

3.       Designing WCF Service Contracts

4.       How to: Define a Windows Communication Foundation Service Contract

5.       WCF Data Contracts

 

 

 

What is Address Header in WCF?

 

Address Header in WCF Service has information which gets sent with every request. It can be used to include any processing logic or routing logic by the end point services.

WCF has the AddressHeader Class for this. This class represe