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
-
- {
-
- ....................................
-
- }
Name
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
-
- {
-
- ................................
-
- }
The Application configuration file would look, as shown below.
- <configuration>
- <system.servicemodel>
- <services>
-
- <service name = “Demo”>
- ...................................
- </service>
-
- </services>
-
- </system.servicemodel>
-
- </configuration>
Namespace
This parameter gets or sets <portType> element namespace in WSDL. Its default value is "http://tempuri.org".
Example
- [ServiceContract(Namespace=”http:
-
- public interface interfaceName
-
- {
-
- ..................................
-
- }
ProtectionLevel
This parameter is used for protection level bindings, which includes an encryption and digital signature. The value of this parameter can be set by following ProtectionLevel enumeration values.
ProtectionLevel Enumeration values | Brief Descriptions |
EncryptAndSign | Encrypt and sign data to ensure the integrity and confidentiality |
None | Only simple authentication |
Sign | No encryption only ensures integrity |
The default value of this parameter is EncryptAndSign, if binding supports security and None, if bindings do not support security.
Example
- [ServiceContract(ProtectionLevel = System.Net.Security.ProtectionLevel.None)]
-
- public interface interfaceName
-
- {
-
- .............................
-
- }
SessionMode
This parameter specifies the supports of session for ServiceContract. The value of this parameter can be set by following SessionMode enumeration values given below.
SessionMode Enumeration values | Brief Descriptions |
Allowed | Specifies session support for contract |
NotAllowed | Specifies that contract does not support session |
Required | Specifies that contract requires session all the time |
Example
- [ServiceContract(SessionMode = SessionMode.Required)]
-
- public interface interfaceName
-
- {
-
- ............................
-
- }