【发布时间】:2012-05-13 07:07:38
【问题描述】:
根据我的理解,以下内容应该正确地从 WCF Rest 服务中引发自定义错误:
[DataContract(Namespace = "")]
public class ErrorHandler
{
[DataMember]
public int errorCode { get; set; }
[DataMember]
public string errorMessage { get; set; }
} // End of ErrorHandler
public class Implementation : ISomeInterface
{
public string SomeMethod()
{
throw new WebFaultException<ErrorHandler>(new ErrorHandler()
{
errorCode = 5,
errorMessage = "Something failed"
}, System.Net.HttpStatusCode.BadRequest);
}
}
在 Fiddler 中这似乎可行,我得到以下原始数据:
HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Length: 145
Content-Type: application/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Set-Cookie: ASP.NET_SessionId=gwsy212sbjfxdfzslgyrmli1; path=/; HttpOnly
X-Powered-By: ASP.NET
Date: Thu, 03 May 2012 17:49:14 GMT
<ErrorHandler xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><errorCode>5</errorCode><errorMessage>Something failed</errorMessage></ErrorHandler>
但现在在我的客户端我有以下代码:
WebChannelFactory<ISomeInterface> client = new WebChannelFactory<ISomeInterface>(new Uri(targetHost));
ISomeInterface someInterface = client.CreateChannel();
try
{
var result = someInterface.SomeMethod();
}
catch(Exception ex)
{
// Try to examine the ErrorHandler to get additional details.
}
现在,当代码运行时,它会遇到问题并且是 System.ServiceModel.ProtocolException,并带有消息“远程服务器返回了意外响应:(400) 错误请求。”。此时我似乎无法查看 ErrorHandler 的详细信息。有没有人遇到过这个?现在有什么方法可以获取 ErrorHander 详细信息吗?
【问题讨论】:
标签: wcf rest webchannelfactory