I'm using WCF REST service with API key template and trying to enforce validation using Validation Application Block attribute validation. here is my service:
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
[ValidationBehavior]
public class Service1
{
[FaultContract(typeof(ValidationFault))]
[WebGet(UriTemplate = "ValidateStuff?text={text}")]
public void ValidateStuff(
[NotNullValidator]
string text)
{
}
and the global.asax from the template:
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
// Edit the base address of Service1 by replacing the "Service1" string below
RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
}
}
then I have a client sending a GET request:
HttpWebRequest invokeRequest = WebRequest.Create(String.Concat(baseUrl, "/", uri, queryString)) as HttpWebRequest;
invokeRequest.Method = Enum.GetName(typeof(Method), method);
WebResponse response = invokeRequest.GetResponse())
now the problem is that I get HTTP/1.1 500 Internal Server Error everytime.
if I remove the [ValidationBehavior] [FaultContract(typeof(ValidationFault))] and [NotNullValidator] attributes then everything works just fine. I checked service trace and didnt see anything that can help me.
The answer is that it is indeed possible! I found out what's the problem. I was missing references to:
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Interception.dll
The strage thing is that I didn't see any indication for that in the trace log nor debug mode. The only way I found out was when I was trying to do the validation manually in the operation implementation itself, like so:
//at this point I got the exception saying that I'm missing the above references.
var validationResult = Validation.Validate<T>(TInstance);
Hope it helps somebody