Soap FaultString显示详细的错误消息而不是短错误

时间:2013-10-22 20:57:44

标签: service asmx

我有一个asmx服务和一个抛出soap异常的方法。问题是当发生异常时我得到以下故障字符串。

<faultstring>System.Web.Services.Protocols.SoapException: Error occurred at CustomServices.Application.TestService.ThrowSoapException(XmlNode ErrorNode) at CustomServices.Application.TestService.MyMethod(String param1, String param2, String param3)</faultstring> 

但我想要的只是我传递给SoapException构造函数的字符串“Error Occurred”。像这样的东西

<faultstring>Fault occurred</faultstring> 

现在可以使用的一个选项是我可以将自定义错误模式设置为On并且它只显示短消息,但是我想知道是否有任何其他方法来执行此操作并在代码本身中阻止它而不是更改配置文件的自定义模式。以下是我抛出soap异常的代码

 public void WebServiceExceptionHandler(XmlNode node)
    {
        System.Xml.XmlDocument Xmldoc;
        System.Xml.XmlNode xmlNode;
        try
        {
            Xmldoc = new System.Xml.XmlDocument();
            xmlNode= doc.CreateNode(XmlNodeType.Element, SoapException.DetailElementName.Name, SoapException.DetailElementName.Namespace);

          //my detaild error node to be displayed.
            XmlNode errorNode= Xmldoc.ImportNode(node, true);
            xmlNode.AppendChild(errorNode);
            //Throw the exception.    
            SoapException se = new SoapException("Error Occurred", SoapException.ServerFaultCode, Context.Request.Url.AbsoluteUri, xmlNode);
            throw se;
        }   

我没有看到任何可以将faulttring限制为我发送的错误的内容,而不是将堆栈跟踪信息添加到它。有人可以帮我这个。我尝试更改soapException类的构造函数,只是传递错误消息和错误代码,但这似乎没有帮助。

1 个答案:

答案 0 :(得分:2)

当customErrors功能设置为Off或RemoteOnly *时,ASP.Net会自动包含异常详细信息,例如异常名称和堆栈跟踪。要更改此设置,您需要更改web.config,如下所示:

<configuration>
    <system.web>
        <customErrors mode="On" />
    </system.web>
</configuration>

进行此更改后,您应该会看到预期的<faultstring></faultstring>


* 如果将customErrors设置为RemoteOnly,则会在本地生成详细消息,但如果请求来自远程计算机,则会发送不太详细的版本。