如何在客户端接受SoapException

时间:2012-05-22 19:37:29

标签: c# winforms web-services asmx soapexception

我得到了可怕的"客户端发现的响应内容类型" 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文件)连接到webservices,如果这有帮助/重要。

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块中调用了#34;返回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>

如果有人知道这个部分的格式很好,请执行:)

2 个答案:

答案 0 :(得分:1)

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

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

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

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

为:

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

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

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

答案 1 :(得分:0)

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