WCF Sample - Attribute driven Constraints

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:

    public DateTime StartDate;

    [GreaterEqualTo(CompareAgainst.FieldOrProperty, "StartDate")]
    public DateTime EndDate;

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


Service Contract Sample:

[ServiceContract, ConstraintsValidatorBehavior]

    int MethodA(
        int a,
        double b,
        string c); 

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

    DateTime MethodC(
        int a);


    void MethodD(
        TestClass a);

Download at wcf.netfx3.com

Comments (1) -

  • Thanks for your marvelous posting! I really enjoyed reading it, you can be a great author.I will make certain to bookmark your blog and may come back from now on. I want to encourage you to continue your great writing, have a nice holiday weekend!

Add comment