Introduction
Windows Communication Foundation (WCF) is the framework for building, configuring and deploying network-distributed services. WCF is the latest service-oriented technology. WCF is a programming platform and runtime system for creating the services. WCF is the unified programming model included in the .NET Framework 3.0. WCF is a collection of various features, like Web Services, Remoting, COM+ and MSMQ. The main feature of WCF is to provide a common platform for all .NET communication. WCF provides a wide range of features, including security and communication protocol support.
Figure 1: WCF Basic diagram
Advantages of WCF
- WCF provides better security and reliability compared to ASMX web Services.
- In WCF, we don't need to change as much in the code for implementing the security model and changing the binding. We just need to make small changes in the configuration to fulfill the requirements.
- WCF is interoperable with the other services compared to .NET remoting where the client and service must be .NET.
Binding in WCF
Binding defines an important role. Binding specifies how a client will communicate with the services and the address where the endpoint is hosted.
Types of bindings in WCF
- BasicHttpBinding
It is the Basic Web service for the communication. No security by default.
- NetMsmqBinding
It provides the communication among WCF applications using queuing.
- NetTcpBinding
It provides the communication among WCF applications across computers. It also supports duplex contracts.
- NetPeerTcpBinding
It provides communication among computers across peer-to-peer services. It supports duplex contracts.
- WSDualHttpBinding
Web Services with a duplex contract and transaction support.
- NetNamedPipeBinding
It provides the communication among WCF applications on the same computer. Its supports duplex contracts and transactions.
Contracts in WCF
The main use of contracts is to allow the client and services agree as to the types of operations and structures they will use during the communication process. It also shows the formal agreements between client and service to define a platform-neutral and standard for describing that what the service does.
WCF has the following four contracts:
- Service Contract
- Message Contract
- Data Contract
- Fault Contract
Service Contract
The main purpose of a Service Contract is to define the interface for the services and the service contract also describes the operation that the service can provide. The service contract is created using the Service and Operational Contract attributes. To create a service contract we define the interface with related methods and decorate the interface with the ServiceContract Attribute to indicate it is a service contract.
Syntax
- [ServiceContract]
- public interface ISevice2
- {
-
- }
Message Contract
The Message Contract is provided by the WCF runtime for the communication between Client and Services. In the Message Contract we can create our own message format so it can be possible using the Message Contract attribute.
Syntax
- [MessageContract]
- public class Person
- {
- [MessageHeader]
- public Operation Name;
-
- [MessageHeader]
- public string Address;
-
- [MessageBodyMember]
- private Home_Phone;
-
- [MessageBodyMember]
- private PersonalID;
-
- [MessageBodyMember]
- public int age;
-
- }
Data Contract
The Data Contract provides the description of the custom data type exposed to the client. So it defines the data types that are passed to and from the service. A Data Contract describes the external format of data passed to and from the service operation. The Data Contract also defines the structure and types of data exchanged in the service message.
- [DataContract]
- public class Student
- {
- private string _Name;
- private string Address;
- [DataMember]
- public string Name
- {
- get
- {
- return Name;
- }
- set
- {
- _Name = value;
- }
- }
- }
Fault Contract
A Fault Contract provides a documented view for errors occurring in the service to the client. A Fault Contract handles the error or exception and understands the cause of an error occuring in the WCF service. By default, when we throw an error from a service, it will not be received by the client side, so to reduce the problem, WCF provides a convey message to the client from the service using the Fault Contract.
- [ServiceContract]
- public interface IGetDetailsService
- {
- [OperationContract]
- [FaultContract(typeof(Student))]
- Student GetDetails(string Name);
- }
-
- [DataContract]
- public class Student
- {
- private string _Name;
-
- private string _Address;
-
- [DataMember]
- public string Name
- {
- get
- {
- return _Name;
- }
- set
- {
- _Name = value;
- }
- }
-
- [DataMember]
- public string Address
- {
- get
- {
- return_Address;
- }
- set
- {
- _Address = value;
- }
- }
-
- }
Summary
This article explained the basics of WCF, contracts of WCF Services and the binding in the WCF Services.
I hope this article will be helpful for the beginners, if they want to start working in the WCF.