FluentValidation and PostSharp for RESTful WCF parameter validation – Part 4

 

This is the last of the series and you can find the previous parts below:

FluentValidation and PostSharp for RESTful WCF parameter validation – Part 1
FluentValidation and PostSharp for RESTful WCF parameter validation – Part 2
FluentValidation and PostSharp for RESTful WCF parameter validation – Part 3

The promised example solution at: https://github.com/naeemkhedarun/WcfValidation 

You will need PostSharp 2.0 installed, the community edition is fine.

This is a functional RESTful WCF service where you can send a product using a JSON request, and have the same product sent back in the response.

[RestArgumentValidation]
public ProductItem Create([ProductItemValidator] ProductItem instance)
{
    return new ProductItem
               {
                   Name = instance.Name
               };
}

On my current project we use a structure similar to the sample project to organise our attributes and validators.

image

To test this service I’m using the REST Console application for Chrome. First we need to set up our target:

image

When we make the request we should get a helpful HTTP Status Code of 400 (Bad Request):

image

We should also get a response body giving us some more detail about the issue with our request.

image

To make a valid request, we simply send the correct request body which looks like:

image

image

Running the request once more we receive the proper response:

image

And there you have it, I hope you’ve found this useful!

blog comments powered by Disqus