我使用REST从第三方Web服务获取数据(使用WebHttpBinding
和WebHttpBehavior
)。当我在查询字符串中传递一个无效参数时,服务以500状态作出响应并描述问题。响应看起来像这样:当使用WCF服务返回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:内部服务器错误”。
我试着创建一个自定义行为并添加了一个消息检查器,但是我的自定义消息检查器代码在引发异常之前没有得到执行(如果没有引发异常,它会得到执行)。
您是否在服务web目录的web.config中添加了'? –
2011-06-14 17:06:40
我只是客户在使用其他人的服务。我无法控制服务。 – dontangg 2011-06-14 17:15:01
您必须联系谁提供此服务,并询问如何获取详细的例外信息。 – 2011-06-14 17:23:26