Well in order to understand the role ServiceContractAttribute
plays in the context of WCF, it is important to first understand the role of Attribute-based programming in .NET.
By applying an Attribute to some entity in your program, you are embedding additional metadata into the assembly for that type, member, etc. But it is essentially useless until a piece of software uses reflection to query that information.
When applying the ServiceContractAttribute
you are adding metadata to indicate that the interface (or class) defines a Service Contract. Using this attribute you can also control the contents of the generated metadata by setting the various properties (Name
, Namespace
, ProtectionLevel
, etc). This information signals to the WCF runtime how to generate the required metadata and WSDL for locating operations that the contract supports as well as managing the actual runtime calls from the clients.
There are several additional bonuses that come from the metadata this attribute emits. A classic example is the runtime check to see if the ProtectionLevel
specified by the contract is being honored by the configured binding. Imagine if the service contract specified ProtectionLevel.EncryptAndSign
and the binding was set to use BasicHttpSecurityMode.None
- this could be a disaster potentially exposing sensitive information.