Welcome to Windows Communication Foundation (WCF)
Top Tasks :

Attribute driven Constraints

File Details
Downloads: 654 File Size: 206.9kB
Posted By: Anonymous Views: 2634
Date Added: Fri, Jul 14 2006

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

·         OneOf

Data Contract Sample:

[DataContract]

class TestClass

{

    [DataMember]

    [LessEqualTo(CompareAgainst.FieldOrProperty,"EndDate")]

    public DateTime StartDate;

 

    [DataMember]

    [GreaterEqualTo(CompareAgainst.FieldOrProperty, "StartDate")]

    public DateTime EndDate;

           

    /// <summary>

    /// Creates a new instance of TestClass

    /// </summary>

    /// <param name="startDate"></param>

    /// <param name="endDate"></param>

    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);

}

 

Comments
 

Adam said:

This does not compile for me with the WCF release. I get the following errors: Error 1 'newtelligence.ServiceModelExtensions.Constraints.ConstraintsValidatorBehavior' does not implement interface member 'System.ServiceModel.Description.IContractBehavior.ApplyDispatchBehavior(System.ServiceModel.Description.ContractDescription, System.ServiceModel.Description.ServiceEndpoint, System.ServiceModel.Dispatcher.DispatchRuntime)' C:\temp\constraints\newtelligence.ServiceModelExtensions.Constraints\ConstraintsValidatorBehavior.cs 17 18 newtelligence.ServiceModelExtensions.Constraints Error 2 'newtelligence.ServiceModelExtensions.Constraints.ConstraintsValidatorBehavior' does not implement interface member 'System.ServiceModel.Dispatcher.IErrorHandler.ProvideFault(System.Exception, System.ServiceModel.Channels.MessageVersion, ref System.ServiceModel.Channels.Message)' C:\temp\constraints\newtelligence.ServiceModelExtensions.Constraints\ConstraintsValidatorBehavior.cs 17 18 newtelligence.ServiceModelExtensions.Constraints Error 3 'newtelligence.ServiceModelExtensions.Constraints.ConstraintsValidatorBehavior' does not implement interface member 'System.ServiceModel.Dispatcher.IErrorHandler.HandleError(System.Exception)' C:\temp\constraints\newtelligence.ServiceModelExtensions.Constraints\ConstraintsValidatorBehavior.cs 17 18 newtelligence.ServiceModelExtensions.Constraints for some reason I can't press return in this comment box either.
Thu, Mar 01 2007 16:13

Add Comment

Name (required)
Web Site (optional)
Comment (required)
Add

Copyright © 2006 Microsoft Corporation. All Rights Reserved. | Terms of Use | Privacy Statement | Contact Us