【问题标题】:How to accept a SoapException on the client side如何在客户端接受 SoapException
【发布时间】:2012-06-14 05:20:54
【问题描述】:

我收到了可怕的“客户端发现响应内容类型为“text/html”但预期为“test/xml”错误,但经过一番谷歌搜索后,我发现this post 关于如何返回 SoapException。

所以我继续实现了它,但现在我仍然遇到同样的错误。我需要在客户端进行更改才能接受 SoapException(我不这么认为,一切都只是继承自 Exception,对吧?)

为什么我仍然在客户端收到可怕的响应?

    [WebMethod]
    [SoapHeader("authenticationHeader")]
    [WebServiceAuthentication(AuthenticationRequired = false)]
    public DataSet GetConversionErrors()
    {
        try
        {
            return Components.PWD.GetConversionErrors();
        }
        catch (Exception ex)
        {
            PublishingManager.Publish(ex, HttpContext.Current.User.Identity.Name);
            throw Components.SoapException.GenerateSoapException(ex, Context);
        }
    }

编辑:我知道这个 catch 块中的异常正是我需要看到的,但我仍然在客户端收到 System.InvalidOperationException。

Edit2:如果有帮助/很重要,我将通过 WebReference(调用 *.asmx 文件)连接到 Web 服务。

Edit3:这是写入我们的异常日志服务器端的内容(在 PublishingManager.Publish() 调用中)。我的想法是这应该是返回给客户的东西......

Exception Details:
Exception Type: System.Exception
Message: Test the text/html text/xml message. 
Method: PWD.Components.PWD.GetConversionErrors 
Username: xxxxxx Created 5/22/2012 4:20:53 PM 
Application: xxxx Application Identity: NT AUTHORITY\IUSR 
Source: xxxx Severity 2 
Machine: xxxxxxxxx IP Address 127.0.0.1 
AppDomain /LM/W3SVC/1/ROOT-1-129821937733610609 
Stack Trace    at PWD.Components.PWD.GetConversionErrors() in C:\Vault\Development-New\Web\Main_Logging\PWD\Components\PWD.cs:line 73 
   at PWD.PWD.GetConversionErrors() in C:\Vault\Development-New\Web\Main_Logging\PWD\PWD.asmx.cs:line 44 
Additional Information:
  +System.Exception: 
    Message: Test the text/html text/xml message. 
    Data: System.Collections.ListDictionaryInternal 
    InnerException:  
    TargetSite: System.Data.DataSet GetConversionErrors() 
    StackTrace:    at PWD.Components.PWD.GetConversionErrors() in C:\Vault\Development-New\Web\Main_Logging\PWD\Components\PWD.cs:line 73 
   at PWD.PWD.GetConversionErrors() in C:\Vault\Development-New\Web\Main_Logging\PWD\PWD.asmx.cs:line 44 
    HelpLink:  
    Source: xxxx 
     UserName: xxxxxxx 
     TypeName: PublishingManager 
     MachineName: xxxxxxxxx 
     TimeStamp: 5/22/2012 4:20:54 PM 
     FullName: Services.Publishing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null 
     AppDomainName: /LM/W3SVC/1/ROOT-1-129821937733610609 
     ThreadIdentity:  
     WindowsIdentity: NT AUTHORITY\IUSR 
     Severity: 2 
     MachineIPAddress: 127.0.0.1 

Edit3:只是想抛出这个额外的代码块,以便人们看到我是如何测试这些异常的(也许这是错误的测试方式......)。在我的第一个代码块中,我在 try 块中调用了“return Components.PWD.GetConversionErrors();”,这是该方法:

    public static DataSet GetConversionErrors()
    {
        DB db = new DB();

        try
        {
            //return db.ExecuteDataset(Configuration.GenericConfig.AppConnectionString, CommandType.Text, spGetConversionErrors);
            throw new Exception("Test the text/html text/xml message.");
        }
        catch
        {
            throw;
        }
    }

如您所见,我已经注释掉了实际调用,并添加了我自己的行,每次测试时都会抛出异常...

Edit4:@gbvb 这是新方法:

    [WebMethod]
    [SoapHeader("authenticationHeader")]
    [WebServiceAuthentication(AuthenticationRequired = false)]
    public DataSet GetConversionErrors()
    {
        try
        {
            return Components.PWD.GetConversionErrors();
        }
        catch (Exception ex)
        {
            //PublishingManager.Publish(ex, HttpContext.Current.User.Identity.Name);
            //throw Components.SoapException.GenerateSoapException(ex, Context);

            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            System.Xml.XmlNode detailNode = xmlDoc.CreateNode(System.Xml.XmlNodeType.Element, System.Web.Services.Protocols.SoapException.DetailElementName.Name, System.Web.Services.Protocols.SoapException.DetailElementName.Namespace);
            detailNode.InnerText = ex.ToString();

            throw new System.Web.Services.Protocols.SoapException(ex.Message, System.Web.Services.Protocols.SoapException.ClientFaultCode, System.Web.HttpContext.Current.Request.Url.AbsoluteUri, detailNode, ex.InnerException);
        }
    }

Edit5:Fiddler2 看到了什么:

HTTP/1.1 500 Internal Server Error
Cache-Control: private
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Wed, 23 May 2012 19:25:56 GMT
Content-Length: 1208

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>500 - Internal server error.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>500 - Internal server error.</h2>
  <h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
 </fieldset></div>
</div>
</body>
</html>

如果有人知道如何很好地格式化此部分,请这样做:)

【问题讨论】:

  • “所以我继续实施,但现在我仍然遇到同样的错误”我以前听说过并说过。您确定您已完全实施您的更改并将它们部署到您正在测试的环境中吗?
  • 此外,了解您正在运行的 .NET 版本也很重要
  • 我相信是的,我正在我的“本地主机”上运行 Web 服务并调用它。我可以在服务器端和客户端都设置断点...但是当它在客户端停止时,从服务器返回的实际异常是 InvalidOperationException 而不是 SoapException。
  • 在大多数情况下,从 ASMX 切换到 WCF 并不难。客户端代码甚至不需要知道开关。此外,今年的转换会比 5 年或 10 年后的转换要容易得多,那时甚至没有人记得 ASMX 曾经是什么。另外,就像我说的,ASMX 不能正确支持故障。
  • 有点像,但那是几年前的事了。特别是,服务配置在 .NET 4.0 中更简单,所以我不确定您是否必须将单独的端点添加到 web.config。如果你这样做了,你可能只需要指定每个端点的 URL 就可以了——其余的都是默认的。此外,在 .NET 4.0 中,您的构建过程不需要更新它们。您将使用 web.config 转换。

标签: c# winforms web-services asmx soapexception


【解决方案1】:

所以,我让它工作了。这是我发现的:

在 IIS 中查看我网站的属性,然后选择“模块”时,有一个名为“CustomErrorModule”的条目正在尝试处理网站/Web 服务引发的错误。

解决方法是删除该条目“CustomErrorModule”,或从以下位置更新我的配置文件:

<httpErrors errorMode="Custom" existingResponse="Replace">

到:

<httpErrors errorMode="Custom" existingResponse="Auto">

感谢大家的帮助,它引导我朝着正确的方向前进。 (顺便说一句,我正在更新配置文件,所以我不必弄乱模块的东西)

另外,here's a link 解释了这种情况。

【讨论】:

    【解决方案2】:

    只是为了咯咯笑,您可以尝试以 SoapException.ClientFaultCode 作为该调用的类型显式抛出 new SoapException() 吗?显然,您在上面显示的代码中有几层可能会失败,而且很容易搞砸(相信我,我知道 :))。并且不要调用其他方法,例如 Publisher.publish.. 这将指向是消息声明(即 ASMX 基础架构)还是您的代码与异常混淆..

    【讨论】:

    • 我将立即尝试这些...我还将尝试仅抛出常规异常而不是 SoapException。
    • 代码编辑似乎正确,但您仍然遇到同样的问题?
    • 正确,仍然看到相同的响应。是因为 JohnSaunders 回答“ASMX 不能正确支持故障”,还是因为该故障与我看到的故障不同?
    • 查看此知识库:support.microsoft.com/kb/942031。并尝试解决方案 7,因为它似乎正是您所看到的...... :)
    • 不是客户端。您需要检查服务器!服务器上的某些内容可能配置错误。它可能是分辨率 4,但无论如何,请检查服务器是否允许服务器实际处理 Web 服务 (asmx)。这似乎是我在这里看到的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2014-04-07
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多