我试图使用WCF和.Net 4构建一个API,但是当我抛出WebProtocolException时,它没有像它应该那样显示一个很好的错误消息,而是将它视为未处理的异常。
我正在使用WebServiceHost2Factory。
是否有其他人使用.net 4的WebProtocolException?
我的电话示例如下
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);
答案 0 :(得分:1)
在Visual Studio 2010中升级我的RESTful项目后,我遇到了同样的问题。作为解决方法,我必须要么:
从传出响应头中删除“Cache-Control” WebOperationContext.Current.OutgoingResponse.Headers.Remove(HttpResponseHeader.CacheControl);
或
WebOperationContext.Current.OutgoingResponse.Headers.Remove(“Cache-Control”); 在抛出WebProtocolException异常之前
或
在正常情况下“抛出WebProtocolException(statusCode,...);”应该设置传出响应的状态代码,但如果定义了“Cache-Control”,则不会设置它。我在大多数响应中使用“Cache-Control”设置为“max-age = 0”以确保所有AJAX请求都将设置为服务器。所以我在WebProtocolException中描述了这个bug的问题。