Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data annotations on WCF service contracts

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

I then consume this service in an asp.net MVC application as a service reference. When I get a list of all the attributes using

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

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

like image 208
Colin Desmond Avatar asked May 04 '09 22:05

Colin Desmond


People also ask

What is data contract in WCF?

A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts.

What is data annotation in C #?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.

What can be used to define a WCF service contract?

A service contract defines the operations which are exposed by the service to the outside world. A service contract is the interface of the WCF service and it tells the outside world what the service can do. It may have service-level settings, such as the name of the service and namespace for the service.

What is DataContract attribute?

[DataContract] attribute specifies the data, which is to serialize (in short conversion of structured data into some format like Binary, XML etc.) and deserialize(opposite of serialization) in order to exchange between the client and the Service.


2 Answers

The attributes will not be serialized when your data contract in sent over the wire. The new attribute that you have created in esentially meta data that is associated with the property and therfore the Type in which the property belongs to. This is not data and will not be available.

I guess that you have added a service reference in your asp.net mvc application, this will, unless specified, create new proxy classes that represent your data contract.

When you add the service reference, if you click on the advanced button make sure that the 'Use existing types' is checked. This ensure that your service will use your existing conract.

This may not be best practice because the client application will have to have knowledge about the Type you are returning from the service. This may be okay if your service is only used by yourself in which case you will need to add a reference to you contract in your asp.net mvc application.

like image 59
Rohan West Avatar answered Sep 30 '22 20:09

Rohan West


The OData team is working in a solution to expose the validation metadata as "vocabularies".

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

like image 31
Jorge Fioranelli Avatar answered Sep 30 '22 19:09

Jorge Fioranelli