WCF Service Contract
Want to learn about service contracts and WCF service contracts? In this article I am going to explain about WCF Service contract in detail with complete explanation about its properties.
WCF Service contract is one of the WCF contracts. Service contracts allows you to create agreement between service and client. It is defined in System.Servicemodel namespace. You can mark Classes or interface as ServiceContracts however it is strongly suggested to use interfaces as they directly model service contracts.
ServiceContractAttribute has below propertiesName
Gets or sets Operation Contract's name. You can specify different name for the service using this property. By default the name of service will be your class name. You should use the name property with some meaningful name as your interface will have prefix "I". The name of the service contract displays in WSDL as portType element.
[ServiceContract(Name="Student") ]
public interface IStudentService
{
[OperationContract(Name="StudentName")]
String GetStudentIdentity(int studentId);
}Namespace
It allows you to set the namespace to your service. The default value for namespace property is http://tempuri.org. Specifying the namespace property other than default helps you to reduce the chance of collisions.
[ServiceContract(Namespace="http://mycompany.com/Students") ]
public interface IStudentService
{
[OperationContract(Name="StudentName")]
String GetStudentIdentity(int studentId);
}CallBackContract
If you are using duplex MEP (Read more on WCF MEP) then callback contract is very important. It specifies the interface which is another service contract as callbackcontract. By using duplex contracts client application can listen inbound operation calls.
[ServiceContract]
public interface IStudentActivities
{
[OperationContract(IsOneWay = true)]
void ShowStudentActivities(Product product);
}
[ServiceContract(CallbackContract=typeof(IStudentActivities))]
public interface IStudentDuplex
{
[OperationContract(IsOneWay = true)]
void GetStudent(int ID);
}ProtectionLevel
It allows you to set protection level of service messages of all operations on wire.
There are three choices to set this property.
None: Messages 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.
The value set to ServiceContract's Protection level property is bound or same to all operations including fault.
[ServiceContract(ProtectionLevel = ProtectionLevel.Sign)]
public interface ISecuredStudent
{
[OperationContract()]
int GetCountOfStudents();
}ConfigurationName
This name will find out the service element details in application config file where you can define service behavior. The default value for this is Service Class.
[ServiceContract(ConfigurationName="StudentConfig")]
public interface IStudents
{
[OperationContract]
string StudentDetails(string name);
}
SessionMode
It indicates whether the session can be enabled on service level. You can set it as
Allowed: Session can be used but not mandatory.
Required: Session has to use by client to initiate service call.
NotAllowed: Session cannot be used with service.