Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set the max-length of a data member in WCF

Tags:

c#

validation

wcf

Is it possible to set a requirement in WCF that a specific string datamember have a maximum length?

I'm essentially wanting to do some basic validation, and enhancing the implicit documentation that WSDL gives you.

I'm pretty sure that its possible when writing raw WSDL, but am not sure if you can do it in WCF using attributes etc.

And related, require that an array property have at least one element in it...

like image 270
Gareth Avatar asked Dec 11 '09 13:12

Gareth


2 Answers

No, to my knowledge, that's not possible - at least not yet. There is that concept of Data annotations floating around, being supported by ASP.NET dynamic data and now also by the Silverlight RIA Services, but in "pure" WCF, I don't know of any such means of limiting the length of a string in your DataContract, or requiring an array to have at least one members.

At least not in a declarative way (i.e. with attributes on your data contract).

There are a few things you can do on the data contract, like require an attribute to be present and such, but those are very limited in scope.

like image 179
marc_s Avatar answered Nov 19 '22 22:11

marc_s


I guess this post is old but it now exists just add the Maxlength attribute

[MaxLength(MaxLength = 100)]
public string Name{ get; set; }
like image 1
Darryl Windsor Avatar answered Nov 19 '22 22:11

Darryl Windsor