In this series of articles, we will discuss the major features of WCF:
A Contract is an agreement between the two parties, WCF contract is between a Service and a Client. In WCF,
Contracts are categorized as behavioral or structural.
-
Behavioral Contract
ServiceContract attribute is used to mark a type as Service Contract that contains operations.
OperationContract attribute is used to mark the operations that will be exposed.
- FaultContract defines what errors are raised by the service being exposed.
-
Structual Contract
DataContract attribute defines types that will be moved between the parties.
MessageContract attribute define the structure of the SOAP message.
Behavioral Contract
A service contract defines the operations which are
exposed by the service to the outside world. A service contract is typically an
interface of the WCF service and it tells the outside world what the
service can do.
- [ServiceContract]
- public interface IService1
- {
-
-
- }
An operation contract is defined within a service
contract. It defines the parameters and return type of an operation. An
operation contract can also defines operation-level settings, like as
the transaction flow of the operation, the directions of the operation
(one-way, two-way, or both ways), and fault contract of the operation.
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- string GetData(int value);
-
- [OperationContract]
- CompositeType GetDataUsingDataContract(CompositeType composite);
-
- }
A Fault Contract is a way to handle an
error/exception in WCF. In C# we can handle the error using try and
catch blocks at the client side. The purpose of a Fault Contract is to
handle an error by the service class and display in the client side.
- [ServiceContract]
- public interface IGetDetailsService
- {
- [OperationContract]
- [FaultContract(typeof(Student))]
- Student GetDetails(string Name);
- }
Structual Contract
The DataContract is a totally different beast --- it define the data type for variables
that are the same as get and set properties but the difference is that a Data Contract in WCF is used to serialize and deserialize the complex
data. It defines how data types are serialized and deserialized. Using
serialization, you can convert an object into a sequence of bytes that
can be transmitted over a network. Using de-serialization, you
reassemble an object from a sequence of bytes that you receive from a
calling application.
The DataMemberAttribute
must then be applied to each member of the data contract type
to indicate that it is a data member, that is, it should be serialized.
- [DataContract]
- public class Student
- {
- private string _Name;
- private string _City;
-
- [DataMember]
- public string Name
- {
- get { return _Name; }
- set { _Name = value; }
- }
-
- [DataMember]
- public string City
- {
- get { return _City; }
- set { _City = value; }
- }
- }
A
MessageContract is an abstraction over a SOAP message that allows you
to explicitly dictate the structure of the underlying message.
The default SOAP message format is provided by the WCF runtime for
communication between the client and the service. If it does not meet
your requirements then we can create our own message format. This can be
done using the Message Contract attribute.
- [MessageContract]
- public class Person
- {
- [MessageHeader] public Operation Name;
- [MessageHeader] public string city;
- [MessageBodyMember] private Home Address;
- [MessageBodyMember] private Home Streat;
- [MessageBodyMember] public int age;
- }
The differences between
DataContract and
MessageContract:
- Using
DataContract, the service can expose the types that it interchanges.
But the XML (SOAP) that is interchanged is not controlled (though
impacted) by it.
- While MessageContract can be used to explicitly define how the XML (SOAP message) will be structured.
Summary
This article gave a brief discussion of WCF Contracts.
References