Windows Communication Foundation (WCF)

Windows Communication Foundation (WCF) is a dedicated communication framework provided by Microsoft. WCF is a part of .NET 3.0. The runtime environment provided by the WCF enables us to expose our CLR types as services and to consume other existing services as CLR types.


In the world there are a lot of distributed communication technologies that exist. Some of them are:
  • ASP.NET Web Services (ASMX)
  • Web Services Enhancements (WSE)
  • Messaging (MSMQ)
  • .NET Enterprise Services (ES)
  • .NET Remoting
Creating and Consuming a Sample WCF Service:
Three major steps are involved in the creation and consumtion of the WCF services. Those are:
  1. Create the Service.(Creating)
  2. Binding an address to the service and host the Service. (Hosting)
  3. Consuming the Service.(Consuming)
  4. Step 1: Creating the Service
In WCF, all services are exposed as contracts. A contract is a neutral way of describing what the service does. Mainly we have four types of contracts:
  1. Service Contract : This contract describes all the available operations that a client can perform on the service..Net uses “System.ServiceModel” Name space to work with WCF services.  ServiceContract attribute is used to define the service contract. We can apply this attribute on class or interface. ServiceContract attribute exposes a CLR interface (or a class) as a WCF contract. 
    OperationContract attribute, is used to indicate explicitly which method is used to expose as part of WCF contract. We can apply OperationContract attribute only on methods, not on properties or indexers.[ServiceContract] applies at the class or interface level. 
    [OperatiContract] applies at the method level.
  2. Data Contract

    This contract defines the data types that are passed into and out of the service.[DataContract] attribute is used at the custom data type definition level, i.e. at class or structure level. 
    [DataMember] attribute is used for fields, properties, and events.
  3. Fault Contract : This contract describes about the error raised by the services. [FaultContract(<>)] attribute is used for defining the fault contracts.
  4. Message Contracts : This contract provides the direct control over the SOAP message structure. This is useful in inter-operability cases and when there is an existing message format you have to comply with.[MessageContract] attribute is used to define a type as a Message type.  [MessageHeader] attribute is used for those members of the type we want to make into SOAP headers
    [MessageBodyMember] attribute is used for those members we want to make into parts of the SOAP body of the message.
  5. Sample Service Creation :

Here “IFirstWCFService” is a service exposed by using the servicecontract attribute. This service exposes two methods “Add”,”Hello” by using the [OperationContract] attribute. The method “Multiplication” is not exposed by using the [OperationContract] attribute. So it wnt be avlible in the WCF service.

More Here