Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Service Contract in WCF

0.00/5 (No votes)
18 Sep 2011 1  
Service contract means the collective mechanisms by which a service’s capabilities and requirements are specified for its consumers. We must say that

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Service contract means the collective mechanisms by which a service’s capabilities and requirements are specified for its consumers. We must say that it define the operations that a service will perform when executed. It tells more thing about service like  message data types, operation locations, the protocols the client will need in order to communicate with the service. 
There are three types of attributes which are used to annotate these type of operations.
1. ServiceContractAttribute
2. OperationContractAttribute
3. MessageParameterAttribute.
 
ServiceContractAttribute : It is used to declare the type as a Service Contract. It can be declared without any parameters but it can also take named parameters.
 
[ServiceContract(Name="MyService", Namespace="http://tempuri.org")]
 public interface IMyService
{

    [OperationContract]
    int AddNum(string numdesc, string assignedTo);
}
 
OperationContractAttribute: It can be applied only on methods. It is used to declare methods which belongs to Service Contract. It controls over the service description and message formats.

MessageParameterAttribute.: It controls how the names of any operation parameters and return values appear in the service description. It controls how both the parameter and return values are serialized to XML request and response elements at the transport layer. We need to use the  Name property because the variable name as it can’t be used as programming language.
 
[OperationContract]
    [return : MessageParameter(Name="reswait")]
    string MyOp([MessageParameter(Name="string")]string s);



 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here