WCF Service Contract Annotations

I have a WCF service that has the [DataContract] class defined in it. Each of the properties has the [DataMember] attribute, and I added a couple of Data Annotation [Required] and [StringLength] attributes to several properties.

Then I use this service in the asp.net MVC application as a link to the service. When I get a list of all attributes with

var attr= from prop in TypeDescriptor.GetProperties(instance).Cast<PropertyDescriptor>() from attribute in prop.Attributes.OfType<ValidationAttribute>() select attribute; 

I see that none of the data annotations went through. Is this a WCF limitation or am I doing something fundamentally wrong here?

+6
wcf data-annotations
source share
2 answers

Attributes will not be serialized when your data is transferred by cable. The new attribute that you created in the essential metadata that is associated with this property and the type in which the property belongs. This is not data and will not be available.

I assume that you have added a service link to your asp.net mpc application, it will, if not specified, create new proxy classes that represent your data contract.

When you add a link to a service, if you click on the advanced button, make sure the "Use existing types" checkbox is selected. This ensures that your service will use your existing project.

This may not be best practice, because the client application will need to know about the type that you are returning from the service. This may be good if your service is used only by you, in which case you will need to add a link to your contract in your asp.net mpc application.

+7
source share

The OData team is working on a solution to display validation metadata as dictionaries.

Additional information: http://www.odata.org/blog/vocabularies

+3
source share

All Articles