Contract
: Contracts are standard way of
describing what the service does . Contract should be an Interface or class but
for best practice use interface.
<endpoint
address="http://localhost:8080/MyService/MyService.svc" binding="wsHttpBinding" contract="MyInterface">
Types of Contracts
- Service Contracts
- Data Contracts
- Fault Contracts
- Message Contracts
Service Contracts :
Describe which operations the client can perform on the service.
A
Service can have more
than one service contract but it should have at least
one Service contract.
There are two types of Service Contracts.
- ServiceContract - This attribute is used to define the Interface.
- OperationContract - This attribute is used to define the method inside Interface.
[ServiceContract]
interface
IMyContract
{
[OperationContract]
string MyMethod( );
}
class MyService : IMyContract
{
public string MyMethod( )
{
return "First Service contract";
}
}
Data Contract
Datacontract is used to communicate
between client and server or formal agreement between a service and a
client that abstractly describes the data to be exchanged.
Implicit = Simple
type such as int, string etc has an implicit data contract.
Explicit = User
defined object are, for which you have
to define a Data contract using [DataContract] and [DataMember] attribute.
[DataContract]
public class Person
{
[DataMember]
public string name;
}
[DataContract]
public class Employee : Person
{
[DataMember]
public int department;
[DataMember]
public string title;
[DataMember]
public int salary;
}
Fault Contract :
This error should be reported to the client in proper manner. Basically when we
develop managed application or service, we will handle the exception using try-
catch block. But these exceptions handlings are technology specific.
[ServiceContract]
interface
IMyContract
{
[OperationContract]
[FaultContract(typeof(MyFaultException))]
string MyMethod( );
}
class MyService : IMyContract
{
public string MyMethod( )
{
return "First Service contract";
}
}
Message Contract
Sometimes
you complete control over the structure of a SOAP message is just as important as control over its
contents then you can user message contracts both header and body content..
Message
is the packet of data which contains important information. WCF uses these
messages to transfer information from Source to destination. WCF uses SOAP (Simple Object Access Protocol)
Message format for communication.
Simplex - It is one way communication. Source will
send message to target, but target will not respond to the message. Request/Replay
- It is two way communications, when source send message to the target,
it will resend response message to the source. But at a time only one can send
a message Duplex - It is two way communication, both source and
target can send and receive message simultaniouly.
[MessageContract]
public class DownlaodStreamItem
{
[MessageHeader]
public Int64 ItemID { set; get; }
[MessageHeader]
public Int64 SizeOfFile { set; get; }
[MessageHeader]
public String Word { set; get; }
[MessageBodyMember]
public Stream Data { set; get; }
}
No comments :
Post a Comment