Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add restrictions to WCF in method/data member

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?

like image 820
Ben2307 Avatar asked Jul 14 '11 08:07

Ben2307


2 Answers

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.

like image 96
Precious Roy Avatar answered Nov 04 '22 07:11

Precious Roy


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?

like image 22
victorvartan Avatar answered Nov 04 '22 09:11

victorvartan