Question

I have an asmx service and one method which throws a soap exception.The problem is when an exception occurs I get the following following fault string.

<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> 

but what I want is just the string that I am passing to the SoapException constructor "Error Occurred". Something like this

<faultstring>Fault occurred</faultstring> 

the one option that works right now is I can set the custom error mode to On and it does only show the short message, however I want to know if there is any other way of doing it and preventing it in the code itself rather than changing the custom mode of the config file. The following is my code to throw the soap exception

 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;
        }   

I donot see anything that can restrict the faultstring to just the error that I send, instead of adding the stack trace info to it. Could somebody please help me with this. I tried changing the constructor of the soapException class to just pass the error message and the faultcode but that does not seem to help.

Was it helpful?

Solution

ASP.Net automatically includes exception details such as the exception name and the stack trace when the customErrors feature is set to Off or RemoteOnly*. In order to change this you need to change your web.config like so:

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

Once you make this change you should see your expected <faultstring></faultstring>


* If customErrors is set to RemoteOnly detailed messages will be generated locally, but the less detailed version will be sent if the request comes from a remote machine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top