I'm new to WCF and I try to add restriction to data member.
For exmple in this method:
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
I want to set max and min length. I know how to add the restriction to the XML code
<xs:restriction base="xs:string">
<xs:minLength value="2"/>
<xs:maxLength value="10"/>
</xs:restriction>
but is there a way to add a restriction straight from the code?
According to MSDN, maxLength, minLength and length etc are ignored. There is no declarative way to enforce what you're asking for, as much as I wish there was. This is one of those places where the cracks between the .NET and XML worlds show. The only method I've found for enforcement is to build a message inspector and apply the transform in there.
Use http://wcfdataannotations.codeplex.com/
Possible duplicates of the question:
DataMember attributes for Data validation
How should I validate parameters passed into my WCF service?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With