【问题标题】:Catching a WebFaultException's details in WCF Rest在 WCF Rest 中捕获 WebFaultException 的详细信息
【发布时间】:2011-04-24 14:11:21
【问题描述】:

我有一个服务器正在抛出 WebFaultException

try
{
    ...
}
catch (InvalidPasswordException)
{

    throw new WebFaultException<String>
                   ("This is a bad password", 
                    HttpStatusCode.Unauthorized);
}

到目前为止一切顺利。但是,当这个 WebFault 被另一个 C# 项目(客户端)捕获时,我不知道如何获取详细信息。

try
{
    HttpWebRequest httpWebRequest = WebRequest.Create(uri) as HttpWebRequest;

    httpWebRequest.Method = verb;
    httpWebRequest.ContentType = "text/xml";
    httpWebRequest.ContentLength = serializedPayload.Length;

    using (StreamWriter streamOut = new StreamWriter(httpWebRequest.GetRequestStream(), Encoding.ASCII))
    {
        streamOut.Write(serializedPayload);
        streamOut.Close();
    }

    // error here on GetResponseStream
    using (StreamReader streamIn = new StreamReader(httpWebRequest.GetResponse().GetResponseStream()))
    {
            string strResponse = streamIn.ReadToEnd();
            streamIn.Close();
            return strResponse;
    }
}
catch (Exception e)
{
   // The exception is of type WebException with unauthorized but don't know where the details are
}

这段代码捕获了一个我找不到详细信息的 WebException,只是未经授权的。

谢谢

更新 1:当我在提琴手中执行请求时,响应正文是详细信息,但由于在读取响应正文之前引发了异常,因此它不会显示。所以问题是我如何阅读响应正文尽管抛出了非 200。

提琴手原始响应:

HTTP/1.1 401 Unauthorized
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 12 Oct 2010 22:42:31 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 100
Cache-Control: private
Content-Type: application/xml; charset=utf-8
Connection: Close

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Email/Password Mismatch</string>

【问题讨论】:

  • 你的客户是什么?它是 Web 应用程序,还是 Silverlight 应用程序?

标签: c# wcf exception rest


【解决方案1】:

我用过这个

 try
            {
              //wcf service call
            }
            catch (FaultException ex)
            {
               throw new Exception( (ex as WebFaultException<MyContractApplicationFault>).Detail.MyContractErrorMessage );                
            }

【讨论】:

    【解决方案2】:

    WebException 有一个 Response 属性,您可以将其强制转换为 HttpWebResponse,然后调用 GetResponseStream。

    在相关说明中,请从 WCF REST Starter 工具包中获取 Microsoft.Http 库,它是比 HttpWebRequest/Response 更好的客户端库。更多详情,我这里有一些博文:http://www.bizcoder.com/index.php/2009/12/08/why-the-microsoft-http-library-is-awesome/

    【讨论】:

      【解决方案3】:

      我只是猜测,但我认为您只需要检查 GetResponse() 结果的 .StatusCode 成员,除非它是 200,否则不要尝试调用 GetResponseStream()。如果它是错误代码(401 in您的情况),那么错误详细信息应该在 GetResponse() 的 .Content 成员中。

      类似:

      var r = httpWebRequest.GetResponse();
      if(r.StatusCode != 200)
      {
          MessageBox.Show(r.Content); // Display the error 
      }
      else
      {
          var streamIn = new StreamReader(r.GetResponseStream());
          string strResponse = streamIn.ReadToEnd();
          streamIn.Close();
          return strResponse;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-10-08
        • 1970-01-01
        • 2011-01-31
        • 2020-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多