捕获响应当服务返回使用WCF一个500错误 [英] Capture response when a service returns a 500 error using WCF
问题描述
我是从第三方Web服务使用REST获取数据(使用的WebHttpBinding
和 WebHttpBehavior
)。当我在查询字符串传递一个参数无效,该服务采用了500的地位响应并介绍了该问题。响应看起来是这样的:
I'm getting data from a 3rd party web service using REST (using WebHttpBinding
and WebHttpBehavior
). When I pass an invalid parameter in the querystring, the service responds with a 500 status and describes the problem. The response looks something like this:
HTTP/1.1 500 Internal Server Error
Date: Tue, 14 Jun 2011 16:12:48 GMT
... more headers
<Error>Invalid integer value for ID</Error>
我真的很希望能够捕捉到该错误消息。我的WCF客户端是这样的:
I would really like to be able to capture that error message. My WCF client looks like this:
public class DataClient : ClientBase<IDataClient>, IDataClient
{
public DataClient (string endpointAddress)
: base(new WebHttpBinding(), new EndpointAddress(endpointAddress))
{
this.Endpoint.Behaviors.Add(new WebHttpBehavior());
}
public string GetData(string id)
{
// This is where the exception is thrown
return base.Channel.GetData(id);
}
}
当我赶上被抛出的异常,该数据是无处可寻。它只是说,System.ServiceModel.CommunicationException:内部服务器错误。
When I catch the exception that is thrown, that data is nowhere to be found. It just says, "System.ServiceModel.CommunicationException: Internal Server Error".
我试图创建一个自定义的行为,并增加了一个消息检查,但我的自定义消息检查代码没有按之前抛出的异常(它确实得到执行,如果没有异常被抛出)'T被执行。
I tried creating a custom behavior and added a message inspector, but my custom message inspector code doesn't get executed before the exception is thrown (it does get executed if no exception is thrown).
推荐答案
我终于想通出。创建自定义的行为,并添加一个消息检查是我需要做的方式。我只需要在 WebHttpBehavior
来增加我的自定义行为。然后,我可以看一下整个响应,并抛出自己的异常,并在它的所有数据!
I finally figured it out. Creating a custom behavior and adding a message inspector is the way that I needed to do it. I just needed to add my custom behavior before the WebHttpBehavior
. Then I can look at the whole response and throw my own exception with all the data in it!
这篇关于捕获响应当服务返回使用WCF一个500错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!