Passing Validation exceptions via WCF REST

When using WCF and Rest one thing you're going to have to consider is the validation of the object, the way to do this is using the Validation Application Block included in the Microsoft Enterprise Library to validate the data transfer objects. This way you can decorate the objects with attributes to validated the objects

Here is an example

public static void Validate<T>(T obj)
{
     ValidationResults results = Validation.Validate(obj);

     if (!results.IsValid)
         
string[] errors = results
          .Select(r => r.Message)
         
.ToArray();

     WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest;
     WebOperationContext.Current.OutgoingResponse.StatusDescription = String.Concat(errors);
}

thanks to Enrico Campidoglio for helping out on this

blog comments powered by Disqus

About the author

You have probably figured out by now that my name is Bryan Avery (if not, please refer to your browser's address field).  Technology is more than a career to me - it is both a hobby and a passion.  I'm an ASP.NET/C# Developer at heart...

Month List