WCF Operation Contract
In this article I am going to explain about WCF Operation contract. It is one of the WCF contracts. The public method marked as [OperationContract] get exposed to service clients. The OperationContractAttribute belongs System.ServiceModel namespace.
OperationContractAttribute has below properties
1. Name
Gets or sets Operation Contract's name. You can specify different name for the operation using this property. By default the name of operation will be your method name.
[ServiceContract ]
public interface IStudentService
{
[OperationContract(Name="StudentName")]
String GetStudentIdentity(int studentId);
}
2. Action And Reply Action
The OperationContract Action and Reply action values are co related messages with operation. It gives you explicit control on header of soap messages and response messages. These properties provide URI as the address of an operation. The action property provides incoming message whereas Reply action provide outgoing message and WCF dispatcher will route it as per the action value of operations.
[ServiceContract(Namespace="http://methods.mycompany.com")]
public interface IStudentService{
[OperationContract(
Action="http://methods.mycompany.com/GetStudent",
Name=" GetStudent ",
ReplyAction="http://methods.mycompany.com/GetStudent"
)]
string StudentDetails(string msg);
}
3. IsOneWay
It is Boolean property when you set it as True your operation uses Oneway MEP. It does not return any data, fault exception. Reply action is not supported if you are using IsOneWay = True. For more detail on MEP please Check
4. Protection Level
It controls how your message will be protected. There are three available choices to set this property.
None: Message will not secured by signing or encrypting it on wire. The message will be flow in plain text.
Sign: The request and response messages will be digitally signed and ensures there is no tampering while message transfer on wire.
EncryptAndSign: Request and response messages will be encrypted and then signed.
If you want to use the protection level as Sign or EnccryptAndSign then you will have to use the binding which supports security and enable it in binding settings otherwise it will throw the exception.
If Bindings security mode has set to transport then the OperationContract's Protection Level has no effect.
[ServiceContract()]
public interface IStudentMarks
{
[OperationContract(ProtectionLevel = ProtectionLevel.Sign)]
Student GetStudentMarks();
}
5. IsInitiating
This is Boolean property. If you set to true whenever new session created for service this operation method will get execute first and then requested operation method. For making use of this property the ServiceContracts Session mode property should be Allowed or Required and you should use the bindings which supports Sessions (Read more on Service Contract).
6. IsTerminating
This is Boolean property. If you set it to true and clients operation call involves the terminating of Session then this operation contract will come in pipeline after requested operation, opposite of IsInitiating.