Contracts in Windows Communication Foundation
WCF is a platform on which developer can build service oriented application. Contract defines how the service behaves and what operations and data is exposes.
The three core WCF contract map with web service standard
- Service contract mapped with WSDL
- Data contract map to XSD
- Message contract map to SOAP
ServiceContract defined in System.ServiceModel namespace can applied to either .NET interface or class. It used to declare type as service contract. If class in service not having ServiceContractAttritute means not expose to the client.We cannot inherit this attribute therefore you need to explicitly ServiceContractAttribute to sub type as a service contract.
Parameter used in service contract
- NameSpace
- Name
- CallbackContract
- ProtectionLevel
- ConfigurationName
- SessionMode
DataContractAttribute
WCF is designed in such a way with widest possible client applications on both Microsoft and non-Microsoft platforms. This attribute is used to serialize data members. Data Contract are unrelated to access scope in managed code, private data members can be serialized and sent anywhere to access publicly. To exchange your messages the data type must be serialized.
You can use other serialization mechanisms other than DataContactAttribute and DataMemeberAttribute in WCF. ISerializable, SerializableAttribute and IXmlSerializable mechanisms can also handle serialization in your application for carrying messages. You can apply more serialization strategies if required by your data.
OperationContractAttribute
OperationContractAttribute is also defined in System.ServiceModel namespace and use to define method belonging to the service. Any method does not have OperationContractAttribute are not service operations and not exposed to client. Parameter of operation contract is as follows:
- Name
- Action
- ReplyAction
- IsOneWay
- ProtectionLevel
- IsInitiating
- IsTerminating
Operation in WCF must have distinct name otherwise the proxies for web service created with the svcutil.exe fail and throw InvalidOperationException. This is because the DataContractserializer relies on CLR namespace to determine data Contract namespace when the namespace is not explicitly specified using DataContractAttribute and DataMemberAttribute attributes. The CLR namespace append with project namespace by default hence it failure.
MessageParameterAttribute
This is residing in System.ServiceModel namespace and controls how the name of any operational parameters and returns value appear in the service description. In other word, how they emitted as a part of the WSDL for the service. Property of MessageParameterAttribute is:
-
Name
FaultContract
This is mechanism for transferring error or fault condition from service to consumer. The SOAP specification includes a definition for SOAP faults, providing structure for message body when error occurs.
WCF FaultException Class is used to translate .net exception into SOAP faults. When service implementation throw fault exception WCF takes care of serializing that back to the consumer as a SOAP fault. This class comes in two forms
FaultException: used to send untyped fault data back to the consumer
FaultException
FaultContractAttribute
The FaultContractAttribute is defined in System.ServiceModel. This attribute enable the service might be issue which fault service should returned when things goes wrong. This attribute applied only in operation contract and cannot inherit. If there is many fault type we can declare this attribute multiple time as per fault type.
|
Resource Fundamental Windows Communication Foundation Concepts |
| Date:- 28 Dec, 2009 |