Problem with WCF ValidationBehavior

Topics: Validation Application Block
Apr 5, 2012 at 3:04 PM

Hi all

I'm trying to implement WCF validation using EntLib 5.0, and have applied the appropriate attributes to my contract (interface) like so:

    [ValidationBehavior]
    [ServiceContract]
    public interface IMyService
    {
        [FaultContract(typeof(ValidationFault))]
        [OperationContract]
        bool ExecuteCommand(CommandBase command);
    }

CommandBase is an abstract base class. The problem I have is that when calling the operation with a concrete type, EntLib only seems to look at the base class and ignores validation attributes present in the concrete class. Is there any way around this?

Thanks in advance

Andy

Apr 8, 2012 at 7:11 AM

The ValidationBehavior uses the types of the method parameters to create the validators that are used to validate the message.  Because the method has CommandBase as the parameter type that is the type that is used for validation.

If you wish to implement using a runtime check then you will need to add some code.  If you wish to stick with the Aspect Oriented approach then you could modify the Enterprise Library source code to create the validator based on the runtime type.  This is just one project so it's not actually a big change.  

The change would be in ValidatorParameterInspector.cs:

        public object BeforeCall(string operationName, object[] inputs)
        {
            ValidationFault fault = new ValidationFault();
            for (int i = 0; i < inputs.Length; ++i)
            {
                var validator = ValidationFactory.CreateValidator(inputs[0].GetType());
                ValidationResults results = validator.Validate(inputs[0]);

                AddFaultDetails(fault, inputValidatorParameterNames[i], results);
            }

            if (!fault.IsValid)
            {
                throw new FaultException<ValidationFault>(fault);
            }

            return null;
        }

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Jul 23, 2012 at 10:53 AM

Hi Randy, 

 

I've similar problem and I did the changes as per your recommendation.

 

weirdly I get this error in 

throw new FaultException<ValidationFault>(fault);
"The creator of this fault did not specify a Reason."

please help
Jul 24, 2012 at 3:44 AM

That seems like the expected message.  Detailed error information should be within the detail.  See http://entlib.codeplex.com/discussions/227030/ and http://entlib.codeplex.com/workitem/32153.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com