It is contracts that that client and service agree as
to the type of operation and structure they will use during communication. it
is a formal agreement between a client and service to define a platform-neutral
and standard way of describing what the service does. WCF defines four types of
contracts
·
Service Contract
·
Data Contract
·
Message Contract
·
Fault Contract
Service
Contract
Service contract describes
the operations, or methods, that are available on
the service endpoint, and exposed to the outside world. A Service contract describes
the client-callable operations (functions) exposed by the service, apart from
that it also describes.
·
location of operations, interface and methods of your service to
a platform-independent description
·
message exchange patterns that the service can have with another
party. might be one-way/request-reply/duplex.
To create a service contract you define an interface
with related methods representative of a collection of service operations, and
then decorate the interface/class with the ServiceContract Attribute to
indicate it is a service contract. Methods in the interface that should be
included in the service contract are decorated with theOperationContract Attribute.
[ServiceContract]
interface ICuboidService
{
[OperationContract]
CuboidDetail
CalculateDetails2(CuboidInfo cInfo);
}
Data
Contract
In one line, data contract describes the data to be exchanged. it is formal
agreement between service and client, that contains
information about the data they will be exchanging. the
important point, which needs to be mentioned here is that the two parties don’t
have to share the same data types to communicate, they only need the share the
same data contracts. Data contract defines which parameter & return type
will be serialized/de-serialized to and from (Binary
<==> XML) in order to be transmitted between one party to another.
Data contracts can be defined by annotating a class, enumeration, or even a
structure, but not an interface.
To define a data contract, you simply decorate a
class or enumeration with [DataContract] attribute, as
shown below.
[DataContract]
public class CuboidInfo
{
}
Message
Contract
WCF uses SOAP message for communication. Most of the
time developer concentrates more on developing theDataContract, Serializing the data, etc. Some time developer will
also require control over the SOAP message format. In that case WCF provides
Message Contract to customize the message as per requirement.
A Message Contract is used to
control the structure of a message body and serialization process. It is also
used to send / access information in SOAP headers. By default WCF takes care of
creating SOAP messages
according to service DataContracts and OperationContracts.
according to service DataContracts and OperationContracts.
So when you need full control on SOAP messages
structure and how serialization happens specially when your service needs to be
interoperable for consuming by different types of clients or service needs to
provide extra layer of security on messages and message parts, Amessage is
nothing but a packet and WCF uses this packet to transfer information from
source to destination. This message contains an envelope, header and body. There are some rules, which
one needs to follow, while working with message contract.
·
When using Message contract type as parameter, Only one parameter can be used in Operation.
·
Service operation either should
return MessageContract type or it should
not return any value.
·
Operation will accept and return only
message contract type. Other data types are not allowed.
Choosing between DataContract and
MessageContract.
90% of the time, DataContract will be
sufficient, You'll only need message contracts if you need to very closely
and very specifically control the layout of your SOAP messages. In most of the time, you don't need to.
A message contract allows you to specifically say
which elements (scalar types or compound types asDataContracts) will be in the SOAP header,
and which will be in the SOAP body.
You might need this if you have a communication
partner, with whom you have agreed to a very specific format and you have to
tweak your SOAP messages to match that given layout exactly. That's just about
the only valid scenario when you'll need to and should use message contracts.
However, sometimes complete control over the
structure of a SOAP message is just as important as control over its contents.
This is especially true when interoperability is
important or to specifically control security issues at the level of the
message or message part. In these cases, you can create a message contract that enables
you to use a type for a parameter or return value that serializes
directly into the precise SOAP message that you need.
So, to making the long story short: always use data contracts, practically
never use message contracts (unless you
really have to).
Fault Contract
Fault Contract provides documented view for error accorded in
the service to client. This help as to easy identity the what
error has occurred, and where. By default when we throw any exception from
service, it will not reach the client side. The less the client knows about
what happened on the server side, the more dissociated the interaction will be,
this phenomenon (not allowing the actual cause of error to reach client). is
known as error masking.
By default all exceptions thrown on the service side always reach the client as FaultException, as by having all service exceptions
indistinguishable from one another, WCF decouples the client from service.
While the default policy of error-masking is best practice of WCF,
but there are times when client is required to respond
to these exceptions in a prescribed and meaningful way. WCF
provides the option to handle and convey the error message to client from
service using SOAP Fault contract. SOAP faults are
based on industry standard that is independent of any technology specific
exceptions, it is a way to map technology specific exceptions to some neutral
error information. So when service meets as unexpected error, instead of
throwing a raw CLR exception (technology specific), service can throw an
instance of the FaultException <T> class.
No comments:
Post a Comment