11

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?

Nate
  • 30,286
  • 23
  • 113
  • 184
Ben2307
  • 1,003
  • 3
  • 17
  • 31

2 Answers2

8

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.

Precious Roy
  • 1,086
  • 1
  • 9
  • 19
  • 2
    Also based on message inspector [How to: Perform Message Validation with Schema Validation in WCF](http://msdn.microsoft.com/en-us/library/ff647820.aspx) – Kirill Jul 15 '11 at 15:57
  • great link, I was looking for something more explicit like that – Precious Roy Jul 15 '11 at 17:04
1

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?

Community
  • 1
  • 1
victorvartan
  • 1,002
  • 2
  • 11
  • 31