【发布时间】:2011-06-14 16:40:14
【问题描述】:
我正在使用 REST(使用 WebHttpBinding 和 WebHttpBehavior)从第 3 方 Web 服务获取数据。当我在查询字符串中传递无效参数时,服务会以 500 状态响应并描述问题。响应如下所示:
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 客户端如下所示:
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:内部服务器错误”。
我尝试创建自定义行为并添加了消息检查器,但我的自定义消息检查器代码在引发异常之前没有执行(如果没有引发异常,它会执行)。
【问题讨论】:
-
你有
<serviceDebug includeExceptionDetailInFaults="true"/>在服务web目录的web.config中添加吗? -
我只是消费别人服务的客户。我无法控制服务。
-
您必须联系提供此服务的人员并询问如何获取详细的异常信息。
-
他已经传递了我想要的信息。我只想知道,“ID 的整数值无效”
标签: c# wcf exception-handling