【问题标题】:How do I extract the inner exception from a soap exception in ASP.NET?如何从 ASP.NET 中的肥皂异常中提取内部异常?
【发布时间】:2010-09-07 02:51:44
【问题描述】:

我有这样一个简单的网络服务操作:

    [WebMethod]
    public string HelloWorld()
    {
        throw new Exception("HelloWorldException");
        return "Hello World";
    }

然后我有一个使用 Web 服务然后调用操作的客户端应用程序。显然它会抛出一个异常:-)

    try
    {
        hwservicens.Service1 service1 = new hwservicens.Service1();
        service1.HelloWorld();
    }
    catch(Exception e)
    {
        Console.WriteLine(e.ToString());
    }

在我的捕获块中,我想做的是提取实际异常的消息以在我的代码中使用它。捕获的异常是SoapException,这很好,但它的Message 属性是这样的......

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Exception: HelloWorldException
   at WebService1.Service1.HelloWorld() in C:\svnroot\Vordur\WebService1\Service1.asmx.cs:line 27
   --- End of inner exception stack trace ---

...InnerExceptionnull

我想做的是提取InnerExceptionMessage 属性(我的示例中的HelloWorldException 文本),有人可以帮忙吗?如果可以避免,请不要建议解析SoapExceptionMessage属性。

【问题讨论】:

    标签: .net asp.net web-services exception soap


    【解决方案1】:

    可能的!

    服务操作示例:

    try
    {
       // do something good for humanity
    }
    catch (Exception e)
    {
       throw new SoapException(e.InnerException.Message,
                               SoapException.ServerFaultCode);
    }
    

    使用服务的客户端:

    try
    {
       // save humanity
    }
    catch (Exception e)
    {
       Console.WriteLine(e.Message);    
    }
    

    只有一件事 - 您需要在 web.config 中设置 customErrors mode='RemoteOnly' 或 'On'(服务项目的)。

    关于 customErrors 发现的功劳 - http://forums.asp.net/t/236665.aspx/1

    【讨论】:

    • 我认为您的意思是 throw new SoapException(e.Message, ... 而不是捕获的异常的 InnerException 的消息。
    【解决方案2】:

    我不久前遇到了类似的事情,blogged about it。我不确定它是否完全适用,但可能是。一旦意识到必须通过 MessageFault 对象,代码就足够简单了。就我而言,我知道详细信息包含一个 GUID,我可以使用它来重新查询 SOAP 服务以获取详细信息。代码如下所示:

    catch (FaultException soapEx)
    {
        MessageFault mf = soapEx.CreateMessageFault();
        if (mf.HasDetail)
        {
            XmlDictionaryReader reader = mf.GetReaderAtDetailContents();
            Guid g = reader.ReadContentAsGuid();
        }
    }
    

    【讨论】:

      【解决方案3】:

      很遗憾,我认为这是不可能的。

      您在 Web 服务代码中引发的异常被编码为 Soap Fault,然后作为字符串传递回您的客户端代码。

      您在 SoapException 消息中看到的只是来自 Soap 故障的文本,它没有被转换回异常,而只是存储为文本。

      如果您想在错误情况下返回有用的信息,那么我建议您从您的 Web 服务返回一个自定义类,该类可以具有包含您的信息的“错误”属性。

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

      【讨论】:

      • 如果没有自定义类就可以实现,为什么还要创建自定义类?请查看我在这篇文章中的回答。
      猜你喜欢
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 2011-07-15
      • 2011-01-12
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多