Search code examples
c#wcfweb-servicesvalidationpostsharp

Best way of validating WCF and WebService method parameter values


I'm going to write a validation component to use it on different projects. I'm not really familiar with any of validation frameworks like Enterprise Library VAB, Fluent, CuttingEdge.Conditions and so many others, however I don't have time to work with all of them to see which is better for my purpose.

I want this component to provide 2 different functionalities for me:

First, I want to have some validators like EmailValidator, StringLengthValidator, MyCustomValidator and so on, that I can use them whenever I want in the code, like below:

public class EmailValidator : RegexValidator // or StringValidator or whatever!
{
    public EmailValidator() : base("emailRegexHere")
    {
    }
public bool override DoValidate(string value)
    {
        return base.DoValidate(value);
    }
}
...

public void MyMethod(string email)
{
    EmailValidator validator = new EmailValidator();
    if(!validator.Validate(email))
        throw new NotValidatedException("email is invalid.");
    ...
}

Second, I need to validate parameters by applying something like DataAnnotations to any method parameter that I want without any extra coding. One possible way I know is writing Aspects using PostSharp to inject code where method starts (OnMethodEntry). I've done Logging with Postsharp and it works great.

Also Microsoft introduces IParameterInspector to Perform Input Validation in WCF which provide 2 methods BeforCall and AfterCall, but I think it only works for WCF.

To wrap up, I need to do validation in my WCF or WebService like this:

[System.Web.Script.Services.ScriptService]
public class MyServiceClass : System.Web.Services.WebService
{
    [Aspects.Validate]
    [WebMethod(EnableSession = true)]
    public string SubmitComment([Validation.Required]string content,[Validation.Guid] string userId,[Validation.Required] [Validation.Name]string name, [Validation.Email]string email, string ipAddress)
    {
        ...
    }
}

Note: this is just a sample code to demonstrate the behavior that I need, any other suggestions is well appreciated. Also is it a good idea that Validation.* annotations be changed to one annotation like ValidateParam(typeof(EmailValidator)) ?

Thanks in advance


Solution

  • Yep, I would just look at PostSharp for this. An OnMethodBoundaryAspect or MethodInterceptionAspect that examines the method's parameters (for the attributes that say how to validate) and the arguments (the values you want validate).

    Since you've used OnMethodBoundaryAspect before, take a look at the documentation for MethodExecutionArgs. You can get the method information with args.Method (returns a MethodBase, which is in System.Reflection). Call GetParameters() on that, which returns an array of ParameterInfo. On each ParameterInfo object, you can get information about the attributes with the Attributes property.

    Once you know the attributes, you then know which validation methods to use (or if you're creating your own attributes, the validation method can be in those attribute classes themselves). Then you just need to use args.Arguments to get the values of the arguments.

    Here's some (psuedo)code:

    public interface IValidator
    {
        void Validate(object value);
    }
    public class ValidationEmailAttribute : Attribute, IValidator
    {
        public Validate(string emailAddress)
        {
            // throw exception or whatever if invalid
        }
    }
    
    public class ValidateParametersAspect : OnMethodBoundaryAspect
    {
        public override OnEntry(args)
        {
            foreach(i = 0 to args.Method.GetParameters().Count)
            {
                var parameter = args.Method.GetParameters()[i];
                var argument = args.Argument[i]; // get the corresponding argument value
                foreach(attribute in parameter.Attributes)
                {
                    var attributeInstance = Activator.CreateType(attribute.Type);
                    var validator = (IValidator)attributeInstance;
                    validator.Validate(argument);
                }
            }
         }
    }
    
    public class MyClass
    {
        [ValidateParametersAspect]
        public void SaveEmail([ValidationEmail] string email)
        {
            // ...
        }
    }
    

    That's not real code: the details you'll have to work out yourself.

    I like the idea of putting the validation code in the attributes themselves, because you won't have to change the aspect in order to add additional validation types.