Throw SoapException在响应内容中不包括XmlNode

时间:2019-01-15 17:51:59

标签: c# asp.net .net soap asmx

在我的ASP.NET Web服务中,我抛出了SoapException,并提供了XmlNode。 问题在于浏览器仅发送调用堆栈信息(customErrors已关闭或打开时仅“发生错误”)。

我的问题类似于此处描述的问题: Throwing a SoapException does not return a valid SOAP response 但是,链接Throwing SoapException in .Net web service中提供的解决方案无法解决问题。

asmx服务如下:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class AccountInfoService : System.Web.Services.WebService, IAccountInfoServicesSoap
{
    public void SomeCall(string someParam)
    {
        ThrowSoapError(errorOverride.ErrorCode, errorOverride.ErrorMessage);
    }

    private void ThrowSoapError(int errorCode, string errorMessage)
    {
        if (string.IsNullOrWhiteSpace(errorMessage))
            errorMessage = "Error " + errorCode;

        log.InfoFormat(errorMessage);

        var doc = new XmlDocument();
        var node = doc.CreateNode(XmlNodeType.Element, SoapException.DetailElementName.Name, SoapException.DetailElementName.Namespace);

        var details = doc.CreateNode(XmlNodeType.Element, "Error", "http://tempuri.org/");

        var errorNumber = doc.CreateNode(XmlNodeType.Element, "ErrorNumber", "http://tempuri.org/");
        details.AppendChild(errorNumber);

        var errorNumberChild = doc.CreateNode(XmlNodeType.Text, "ErrorNumber", "http://tempuri.org/");
        errorNumberChild.Value = errorCode.ToString();
        errorNumber.AppendChild(errorNumberChild);

        var errorMessageNode = doc.CreateNode(XmlNodeType.Element, "ErrorMessage", "http://tempuri.org/");
        details.AppendChild(errorMessageNode);

        var errorMessageChild = doc.CreateNode(XmlNodeType.Text, "ErrorMessage", "http://tempuri.org/");
        errorMessageChild.Value = errorMessage;
        errorMessageNode.AppendChild(errorMessageChild);

        node.AppendChild(details);

        throw new SoapException("Fault occurred", SoapException.ServerFaultCode, "url", node);
    }
}

任何想法为何服务未发送Xml部分? Web.config非常简单,看起来像这样:

<?xml version="1.0"?>
<configuration>
  <appSettings>

    <!-- All calls will return this code if set -->
    <add key="error_override_code" value="2100"/>
    <!-- An optional error message if the above value is present -->
    <add key="error_override_message" value="Payment rejected"/>

  </appSettings>
  <connectionStrings/>
    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
        <customErrors mode="Off"/>
    </system.web>
</configuration>    

谢谢Radek

0 个答案:

没有答案