tags:

views:

232

answers:

1

Im trying to build an API using WCF and .Net 4, however when I throw the WebProtocolException, it is not displaying a nice error message like it should, instead it is treating it like an unhandled exception.

I am using the WebServiceHost2Factory.

Has anyone else used WebProtocolException with .net 4?

An example of my call is below

throw new WebProtocolException(System.Net.HttpStatusCode.BadRequest, "The DateFrom parameter is invalid",
new Error()
{
    Code = 6002,
    Message = "Please ensure your dates are in the following format: yyyy/MM/dd hh:mm:ss"
},
null, null);
+1  A: 

I have the same problems after upgrade my RESTful project in Visual Studio 2010. As a workaround I have to either:

  • remove "Cache-Control" from the outgoing response header with respect of WebOperationContext.Current.OutgoingResponse.Headers.Remove (HttpResponseHeader.CacheControl);

    or

    WebOperationContext.Current.OutgoingResponse.Headers.Remove ("Cache-Control"); before throwing WebProtocolException exception

or

  • explicitly set status code in the outgoing response with respect of WebOperationContext.Current.OutgoingResponse.StatusCode = statusCode; before throwing WebProtocolException(statusCode, ...) exception

In the normal case "throw WebProtocolException(statusCode, ...);" should set status code of the outgoing response, but it will be not set if "Cache-Control" is defined. I use in the most responses "Cache-Control" set to "max-age=0" to be sure that all AJAX requests will be set to server. So I had described problems with this bug in WebProtocolException.

Oleg