Background:
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
Three major steps are involved in the creation and consumtion of the WCF services. Those are:
- Create the Service.(Creating)
- Binding an address to the service and host the Service. (Hosting)
- Consuming the Service.(Consuming) Step 1: Creating the Service
- 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. - 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. - Fault Contract : This contract describes about the error raised by the services. [FaultContract(<
>) ] attribute is used for defining the fault contracts. - 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. 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
Courtesy:http://codes4all.wordpress.com/2010/11/25/windows-communication-foundation-wcf/