Tuesday, May 22, 2007

* Windows Communication Foundation (WCF) - A Primer - I


As the name explains :

"It is the technology platform on windows to enable applications so that they can communicate with each other in various formats, protocols and with various levels of coupling. It also enables windows based applications to expose/consume standard based interfaces across other platforms in form of web services."

Although it has been written from scratch it replaces various earlier .NET distributed computing technologies like remoting and consolidate them into one platform. It is interoperable with WSE 3.0, System.Messaging, .NET Enterprise Services, and ASMX Web services.

It uses schemas and contracts instead of classes & types to define and implement communication.



WCF services expose endpoints that clients and services use to exchange messages.

Each endpoint consists of an address, a binding, and a contract.

Address : It specifies where service is located and format is network protocol specific e.g. http or tcp.

Binding : It specifies transport protocol, security requirements & message encoding to be used by client & service for communication.

Contract : It defines what the service can do. WCF service publish multiple types of contracts like service contract, message contract, data contract etc.

WCF has primarily three types of contracts :
service contract
This contract defines the name of the service, its namespace, and other global attributes. The contract is defined by creating an interface and applying the ServiceContractAttribute attribute to the interface.
operation contract
The operation contract defines the parameters and return type of an operation. When creating an interface that defines the service contract, you also define the service operation contracts by applying the OperationContractAttribute attribute to each method definition.
data contract
The data types used by a service must be described in metadata to enable others to interoperate with the service. The descriptions of the data types are known as the data contract, and the types may be used in any part of a message, for example as parameters or return types.

WCF is basically a message based architecture where clients and servers communicate using messages instead of remote invocation like DCOM.

Currently WCF supports three message patterns :

One way messaging : client sends a message to service without expecting a response back

Request Response : client sends a message and waits for reply

Duplex Messaging : client & service send message to each other without the synchronization as required in request – response.



Following are the various options available today for hosting WCF service with the features provided by them :

1. Self Hosted : Console application, Easy to deploy, not recommended for production environments.

2. Windows Service : no IIS required, Robust environment, message-activation not supported, recommended for light weight/use services.

3. IIS 5.1, IIS 6.0 : all the health controlling/monitoring features of IIS available, support HTTP only, scalable.

4. Windows Activation Service (WAS) : available from Longhorn/Vista, no IIS required, health controlling/monitoring features available.

5. IIS 7.0 : all the WAS benefits available, recommended if asp.net content needs to be executed.

To summarize, if Operating System is Windows Server 2008 (Longhorn)/Vista, WAS is the most recommended host.



Bindings contain details which are required by an endpoint of WCF Service. The information contained in Bindings can be classified mainly into three categories :

1. Protocols : details of security mechanism, transaction context, reliable messaging requirements etc.

2. Encoding : details about message encoding like text or binary.

3. Transport : Transport protocol to use like http or binary.

The last two elements are mandatory parts of any binding.

WCF framework comes with some pre-configured binding which can be directly used in applications without mentioning the details about them.

Some of the bindings available are :

1. basicHttpBinding : HTTP protocol binding confirming to WS-I profile specification.

2. wsHttpBinding : confirming to WS-* protocol.

3. NetNamedPipeBinding : for connecting endpoints on same machine.

4. NetMsmqBinding : uses queued message connections.

5. NetTcpBinding : optimized binding for cross machine communication

Custom Binding can be created to handle more complex requirements.

The main features of any binding are :

1. Interoperability Type : kind of integration possible like WS, .NET, peer, MSMQ, etc.

2. Security : Level of security e.g. Transport, Message or Mixed.

3. Encoding : Type of encoding supported e.g. Text, Binary, MTOM.



Securing Services involves four aspects mainly :

1. Authentication : Who the client is and whether it is allowed to communicate with service.

2. Confidentiality : Encrypting communication between client and service.

3. Integrity : To make sure that the communication is tamper proof.

4. Authorization : The access or execution rights of the client with respect to the service.

The various modes of security supported in WCF to implement above requirements are :

1. Transport Mode : The underlying transport protocol like http take care of all the above requirements by default.

2. Message Mode : In this mode all the data required to satisfy above requirements flow as part of message headers.

3. Hybrid Mode : In the mode Confidentiality & Integrity requirements are taken care by Transport mode while Authentication & Authorization are implemented using Message Mode. This mode is also called 'Transport with Message Credentials'.

There are two additional modes that are specific to two bindings : the 'transport-credentials only' mode found on the BasicHttpBinding and the 'both' mode found on the NetMsmqBinding.


SAMPLE CONTRACT DEFINITION                                          


 [ServiceContract()] public interface IBank { [OperationContract] Account Deposit(Account depAccount, int amount); [OperationContract] Account GetBalance(string accountNum); [OperationContract] Account WithDraw(Account wdAccount, int amount); }


 [DataContract] public class Account { string accountNumber; int balance; [DataMember] public string AccountNumber { get { return accountNumber; } set { accountNumber = value; } } [DataMember] public int Balance { get { return balance; } set { balance = value; } } }


SAMPLE CONTRACT IMPLEMENTATION                                

    public class BankService : IBank


        public Account Deposit(Account depAccount,int depValue)


            depAccount.Balance = depAccount.Balance + depValue;

            return depAccount;


        public Account WithDraw(Account wdAccount, int wdValue)


            wdAccount.Balance = wdAccount.Balance - wdValue;

            return wdAccount;


        public Account GetBalance(string accountNum)


            Account acnt = new Account();

            acnt.AccountNumber = accountNum;

            acnt.Balance = 12345;

            return acnt;




1 comment:

  1. WCF also provides test clients. so you actually dont have to host it. Just run the service and it will launch the test client.

    I mean this is just for testing purpose.