WCF Rest服务错误处理和WebChannelFactory

5

据我了解,以下代码应该可以在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) Bad Request.”。看起来我无法在这个点上查看ErrorHandler的详细信息。有人遇到过这种情况吗?是否有办法在此时获取ErrorHandler的详细信息?

1个回答

4
WebChannelFactoryChannelFactory只会向您披露通用的CommunicationException。您需要使用IClientMessageInspector行为或依靠WebRequest返回实际错误。

对于IClientMessageInspector方法-请参阅此博客文章中的评论。

对于WebRequest方法,您可以使用以下内容捕获WebException

try { }
catch (Exception ex)
{
    if (ex.GetType().IsAssignableFrom(typeof(WebException)))
    {
        WebException webEx = ex as WebException;
        if (webEx.Status == WebExceptionStatus.ProtocolError)
        {
            using (StreamReader exResponseReader = new StreamReader(webEx.Response.GetResponseStream()))
            {
                string exceptionMessage = exResponseReader.ReadToEnd();
                Trace.TraceInformation("Internal Error: {0}", exceptionMessage);
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接