WCF .Net 4.0 is treating WebProtocolException as an unhandled exception

≯℡__Kan透↙ 提交于 2019-12-11 02:20:37

问题


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:


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.



来源:https://stackoverflow.com/questions/2616669/wcf-net-4-0-is-treating-webprotocolexception-as-an-unhandled-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!