你如何从Web服务中捕获抛出的SOAPexception?

我成功地在我的Web服务中抛出了一些soapexception。 我想捕获exception并访问被exception调用的字符串和ClientFaultCode。 以下是我在Web服务中的一个例外情况的示例:

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

在我的客户端,我尝试从可能引发exception的Web服务运行该方法,并且我抓住了它。 问题是我的catch块没有做任何事情。 看这个例子:

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

如何访问使用抛出exception调用的字符串和ClientFaultCode?

捕获SoapException实例。 这样你就可以访问它的信息:

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

您可能希望捕获特定的exception。

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