How do I extract the inner exception from a soap exception in ASP.NET?

后端 未结 3 1527
被撕碎了的回忆
被撕碎了的回忆 2021-01-08 00:08

I have a simple web service operation like this one:

    [WebMethod]
    public string HelloWorld()
    {
        throw new Exception(\"HelloWorldException\"         


        
3条回答
  •  温柔的废话
    2021-01-08 01:02

    Unfortunately I don't think this is possible.

    The exception you are raising in your web service code is being encoded into a Soap Fault, which then being passed as a string back to your client code.

    What you are seeing in the SoapException message is simply the text from the Soap fault, which is not being converted back to an exception, but merely stored as text.

    If you want to return useful information in error conditions then I recommend returning a custom class from your web service which can have an "Error" property which contains your information.

    [WebMethod]
    public ResponseClass HelloWorld()
    {
      ResponseClass c = new ResponseClass();
      try 
      {
        throw new Exception("Exception Text");
        // The following would be returned on a success
        c.WasError = false;
        c.ReturnValue = "Hello World";
      }
      catch(Exception e)
      {
        c.WasError = true;
        c.ErrorMessage = e.Message;
        return c;
      }
    }
    

提交回复
热议问题