ServiceContract attribute
[ServiceContract] attribute is used to define a Service contract. This attribute is placed on an interface or a class , which you want to identify as a Service contract. The information specified by the parameters of ServiceContract attribute and its interface is being mapped with the <portType> element in WSDL (Web Service Description Language).
- [ServiceContract]
- public interface interfaceName
- {
- ……………………..
- }
[ServiceContract] attribute parameters
[ServiceContract] attribute contains parameters, which specify additional information about ServiceContract attribute. The name of these parameters and their functions are given below.
In case of two way (duplex) communication CallbackContract parameter gets or sets the callback contract for the client and in case of one way communication, it represents the outgoing calls from the Service to the client. The default value is null reference of this parameter.
Example
- [ServiceContract(CallbackContract = typeof(callbackContractName))]
- public interface interfaceName
- {
- ....................................
- }
This parameter gets or sets the name for <portType> element in WSDL. WSDL (Web Service Description Language) is an XML base format used for the Web Services, which defines the Service location (addresses or endpoints) and its methods. <portType> element in WSDL contains information about the operations, which can be performed and the messages that are involved. Its default value is the name of an interface to which it is applied.
Example
- [ServiceContract(Name=”interfaceName”)]
- public interface interfaceName
- {
- .................................
- }
ConfigurationName
This parameter gets or sets the name, which is used to find the Service element in the configuration file. Its default value is the name of the class, which implements the Service contract.
Example
- [ServiceContract(ConfigurationName = “service”)]
-
- public interface IDemo
-
- {
-
- ................................
-
- }
- [ServiceContract(ConfigurationName = “service”)]
- public interface IDemo
- {
- ................................
- }
The Application configuration file would look, as shown below.

Join the conversation! Your thoughts help the community grow.