This project is read-only.
Project Description
WCFDataAnnotations allows you to automatically validate WCF service operation arguments using the attributes and IValidatableObject interface from System.ComponentModel.DataAnnotations.


(1) Install the NuGet package using the Install-Package WCFDataAnnotations command. Alternatively, download the dll from here and manually add a reference to DevTrends.WCFDataAnnotations.dll in the project containing your service implementation.

(2) Decorate your service implementation with the ValidateDataAnnotationsBehavior attribute:

public class MembershipService : IMembershipService

If you prefer, instead of using the attribute, you can add the behavior in configuration:

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
        <add name="validateDataAnnotationsBehavior" 
             type="DevTrends.WCFDataAnnotations.ValidateDataAnnotationsBehaviorExtensionElement, DevTrends.WCFDataAnnotations, Version=, Culture=neutral, PublicKeyToken=null" />

(3) Add a reference to the .NET 4.0 assembly System.ComponentModel.DataAnnotations in the project containing your data contracts.

(4) Decorate your data contracts with the relevant data annotations.

    [DataContract(Namespace = "")]
    public class AddCustomerAffiliationRequest
        public string ThirdPartyIdentifier { get; set; }       

        [StringLength(500, MinimumLength = 5)]
        public string Description{ get; set; }


You can also/instead implement IValidatableObject:

    [DataContract(Namespace = "")]
    public class AddCustomerToPromotionRequest : IValidatableObject
        public int PromotionId { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
            if (PromotionId < 1)
                yield return new ValidationResult("PromotionId must be greater than 0", new[] { "PromotionId" });

That's it. Connect to your WCF service and your operation arguments will automatically be validated using your configured attributes. If you try to pass invalid data, a FaultException will be thrown detailing the validation failure(s).

For more information about how WCF Data Annotations works and the design decisions behind it, please visit the DevTrends Blog.

Last edited Dec 30, 2011 at 10:27 PM by DevTrends, version 5