WCF.Net 4.0 treats WebProtocolException as unhandled exception

I'm trying to build an API using WCF and .Net 4, however, when I throw a WebProtocolException it doesn't display a nice error message as it should, it instead handles it as an unhandled exception.

I am using WebServiceHost2Factory.

Has anyone else used WebProtocolException since .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);

      

+2


source to share


1 answer


I have the same problems after upgrading my RESTful project in Visual Studio 2010. As a workaround, I should either:

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

    or



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

or



  • explicitly state the status code in the outgoing response in relation to WebOperationContext.Current.OutgoingResponse.StatusCode = statusCode; before throwing WebProtocolException (statusCode, ...)

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

+1


source







All Articles