简体   繁体   English

你如何从Web服务中捕获抛出的SOAP异常?

[英]How do you catch a thrown soap exception from a web service?

I throw a few soap exceptions in my web service successfully. 我成功地在我的Web服务中抛出了一些soap异常。 I would like to catch the exceptions and access the string and ClientFaultCode that are called with the exception. 我想捕获异常并访问被异常调用的字符串和ClientFaultCode。 Here is an example of one of my exceptions in the web service: 以下是我在Web服务中的一个例外情况的示例:

throw new SoapException("You lose the game.", SoapException.ClientFaultCode);

In my client, I try to run the method from the web service that may throw an exception, and I catch it. 在我的客户端,我尝试从可能引发异常的Web服务运行该方法,并且我抓住了它。 The problem is that my catch blocks don't do anything. 问题是我的catch块没有做任何事情。 See this example: 看这个例子:

try
{
     service.StartGame();
}
catch
{
     // missing code goes here
}

How can I access the string and ClientFaultCode that are called with the thrown exception? 如何访问使用抛出异常调用的字符串和ClientFaultCode?

You may want to catch the specific exceptions. 您可能希望捕获特定的异常。

try
{
     service.StartGame();
}
catch(SoapHeaderException)
{
// soap fault in the header e.g. auth failed
}
catch(SoapException x)
{
// general soap fault  and details in x.Message
}
catch(WebException)
{
// e.g. internet is down
}
catch(Exception)
{
// handles everything else
}

Catch the SoapException instance. 捕获SoapException实例。 That way you can access its information: 这样你就可以访问它的信息:

try {
     service.StartGame();
} catch (SoapException e)  {
    // The variable 'e' can access the exception's information.
}
catch (SoapException soapEx) 
{
  //Do something with soapEx
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM