Monday, March 15, 2010

WCF programming for Beginners

Windows Communication Foundation (WCF)

Windows Communication Foundation (WCF) is a dedicated communication frame work provided by the 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 lot of distributed communication technologies 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 while creating and consuming 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)
Step 1: Creating the Service
In WCF, all services are exposed as contracts. Contract is a neutral way of describing the service what it does. Mainly we have four types of contracts:
  1. Service Contract
    This contract describes all the available operations that 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 passed in and out to the service.
    [DataContract] attribute is used at the custom data type definition level, i.e. at class or structure level.[DataMember] attribute is used to 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 to those members of the type we want to make into SOAP headers[MessageBodyMember] attribute is used to those members we want to make into parts of the SOAP body of the message. 
Sample Service Creation :
See full details:

Related links

No comments: