1

I have developed wcf services.I am very interested to know how servicecontract attribute changes the simple interface or classes internally so that it can be exposed to the external world.

I want to know basically how it changes the default structure of a class or interface?

user3611378
  • 43
  • 1
  • 4
  • Usually attributes don't change the structure of the class/property they are applied to. Reflection will be used to reflect over the class finding these attributes and then doing something with them. See the following for a discussion http://stackoverflow.com/questions/2676603/how-do-attribute-classes-work – 3dd May 31 '14 at 04:02
  • Thanks for your help!I want to know the mechanics of serviceattribute i.e how they work? – user3611378 May 31 '14 at 04:08

1 Answers1

1

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.

Derek W
  • 9,708
  • 5
  • 58
  • 67