WCF的.Net 4.0治疗WebProtocolException作为未处理的异常 [英] WCF .Net 4.0 is treating WebProtocolException as an unhandled exception

查看:101
本文介绍了WCF的.Net 4.0治疗WebProtocolException作为未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去建立使用WCF和.NET 4的API,但是当我扔WebProtocolException,它不显示像它应该一个不错的错误信息,取而代之的则是对待它像一个未处理的异常。

我现在用的是WebServiceHost2Factory。

有没有其他人使用WebProtocolException与.NET 4?

我的电话的下面是一个例子

 抛出新WebProtocolException(System.Net.HttpStatus code.BadRequest的DateFrom参数无效
新的错误()
{
    code = 6002,
    消息=请确保您的日期是按以下格​​式:YYYY / MM / DD HH:MM:SS
},
NULL,NULL);
 

解决方案

我升级后同样的问题,我的REST风格的项目在Visual Studio 2010中作为一种变通方法我都可以:

  • 从传出的响应头删除缓存控制与尊重   WebOperationContext.Current.OutgoingResponse.Headers.Remove(Htt的presponseHeader.CacheControl);

    WebOperationContext.Current.OutgoingResponse.Headers.Remove(缓存控制); 抛出之前WebProtocolException异常

  • 明确设置状态code与尊重的输出响应   WebOperationContext.Current.OutgoingResponse.Status code =状态code; 抛WebProtocolException之前(状态code,...)异常

在正常情况下,抛WebProtocolException(状态code,...);应该设置传出响应状态code,但如果将缓存控制是指将未设置。我用最回应缓存控制设置为最大年龄= 0,以确保所有的AJAX请求将被设置为服务器。因此,我所描述的问题,在WebProtocolException这个bug。

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);

解决方案

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.

这篇关于WCF的.Net 4.0治疗WebProtocolException作为未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆