I posted some Bits Michael and I wrote recently in a WCF Project:

The Constraints-Extensions let you easily define attribute based limitations for input, output and return parameters. Limitations can be set to service contracts operations as well as to data contract data member implementations. The constraints validation gets called at runtime by the WCF infrastructure and the behavior will throw a ConstraintViolationException with descriptive messages in case of a constraint gets violated/the validation fails.

The following constraint attributes have been implemented:

  • Between
  • BetweenExclusive
  • CompareAgainst
  • EarlierThanNow
  • EarlierThanToday
  • EqualTo
  • GreaterEqualTo
  • GreaterThan
  • LaterThanNow
  • LaterThanToday
  • LessEqualTo
  • LessThan
  • Match
  • MaxElements
  • MaxLength
  • MinElements
  • MinLength
  • NotBetween
  • NotEmpty
  • NotEqualTo
  • NotNull
  • Nullable
  • OneOff

Data Contract Sample:

[DataContract]  
class TestClass  
{  
    [DataMember]  
    [LessEqualTo(CompareAgainst.FieldOrProperty,<span style="color: maroon;">"EndDate")]  
    DateTime StartDate;

    [DataMember]  
    [GreaterEqualTo(CompareAgainst.FieldOrProperty, <span style="color: maroon;">"StartDate")]  
    DateTime EndDate;

    public TestClass(DateTime startDate, DateTime endDate)  
    {  
        StartDate = startDate;  
        EndDate = endDate;  
    }  
}

 

Service Contract Sample:

[ServiceContract, ConstraintsValidatorBehavior]  
interface ITestConstraints  
{

    [OperationContract]  
    [return:GreaterEqualTo(0)]   
    int MethodA(  
        [Between(0,100)]   
        int a,   
        [LessEqualTo(20)]   
        double b,   
        [NotEmpty]   
        string c); 

    [OperationContract]  
    int MethodB(  
        [Between(0, 100)]   
        int a,   
        [LessEqualTo(20)]   
        ref double b,   
        [NotEmpty]   
        out string c);

    [OperationContract]  
    [return:LaterThanNow]  
    DateTime MethodC(  
        [GreaterThan(0)]   
        int a);

    [OperationContract]  
    void MethodD(  
        [VerifyObject]   
        TestClass a);  
}

Download at wcf.netfx3.com