Property validation

Topics: Validation Application Block
Aug 26, 2008 at 2:46 PM
I have problem validating properties using attributes. I have tried with the following code:

using System;

using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using Microsoft.Practices.Unity;

namespace Validation
    public class Program
        static void Main(string[] args)

           ICustomer customer = new Customer();

                customer.Name = null;
            catch (Exception ex)



    public interface ICustomer
       string Name

            [NotNullValidator(MessageTemplate = "Can not be null")]

    public class Customer : MarshalByRefObject, ICustomer
        public string Name { get; set; }

I would like to get an Exception when i set customer.Name = null. I've tried putting the attributes in the concrete class above the property and also before the set method without success and I can't find any good examples of how to validate properties using . I'm using EntLib 4.
Aug 26, 2008 at 10:56 PM

You need to involve the PIAB in the creation of your Customer instance; this topic should help you

   12             ICustomer customer = PolicyInjection.Create<Customer, ICustomer>();

Next, you need to appropriately target the attributes. Since this is a property, you need to use explicit attribute targets like this:

   25     public interface ICustomer

   26     {

   27         string Name

   28         {

   29             get;

   30             [method: ValidationCallHandler]

   31             [param: NotNullValidator(MessageTemplate = "Can not be null")]

   32             set;

   33         }

   34     }

Finally, you are both implementing an interface and extending MarshalByRefObject. While this won't hurt, it's not necessary to have both to do policy injection.

Hope this helps,
Aug 27, 2008 at 6:19 AM
Sweet, thanks!

Aug 27, 2008 at 8:33 AM
Is there a way to see what property caused the exception? From the Exception I get:

Parameter validation failed
Parameter name: value

for all my properties. Maybe I can use the Exception Handling Application Block to get the message from MessageTemplate?

Aug 27, 2008 at 12:12 PM

The EHAB will not help you here. You specify different message templates for the different properties on the validators themselves, or specify a Tag to the all validators on each property. It would help if the ArgumentValidationException had a property with the name of the method, but I'm not sure it would fit with the design of its base class, ArgumentException. You may want to log feature request for a Method property in the ArgumentValidationException.

Aug 28, 2008 at 2:40 PM
Ok, I got the information from the ArgumentValidationException and the ValidationResults in that exception, it is sufficient for my application. Thank you!
Sep 16, 2008 at 4:30 AM

I too am having issues using Enterprise Lib 4.0 and trying to validate properties.
I have a WCF service and have a property on a class checking for nulls
        [Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator(Ruleset = "WCFNotNull", MessageTemplate = "Price must be between 1 and 50 characters")]
        [WcfSerialization::DataMember(Name = "Price", IsRequired = false, Order = 2)]
        public String Price
            get { return price; }
            set { price = value; }
And a call to validate that class
            Validator<PizzaService.DataContracts.Pizza> validator = ValidationFactory.CreateValidator<PizzaService.DataContracts.Pizza>("WCFNotNull");
            //ValidationResults results = validator.Validate(customer);
            PizzaService.DataContracts.Pizza pizza
                = new PizzaService.DataContracts.Pizza();
            pizza.Price = null;
            ValidationResults results = validator.Validate(pizza);

But it doesn't error.
I have tried just about all the combinations of the Validation Application block that I can find on the internet.
I have not seen a walkthrough for using Enterprise Lib 4.0 with WCF under .net 3.5

I have successful used the above syntax with Enterprise Lib 3.0 with WCF under .net 2.0 (last year).

I don't need best practice or another link to the theory of this.
What I really need is a simple walkthrough or a simple working example of Validation Application Block working with WCF under .Net 3.5.

Does anybody know how to get this working??