2013-09-27 50 views
0

捕获的SoapException我有一个调用的ASMX Web服务的WCF服务。该Web服务抛出,看起来像这样的enxception:WCF客户端从ASMX web服务

 <soap:Body> 
     <soap:Fault> 
      <faultcode>soap:Server</faultcode> 
      <faultstring>System.Web.Services.Protocols.SoapException: error 
         service.method()</faultstring> 
      <faultactor>https://WEBADDRESS</faultactor> 
      <detail> 
       <message>Invalid ID</message> 
       <code>00</code> 
      </detail> 
     </soap:Fault> 
    </soap:Body> 

在C#中我能赶上它作为一个的FaultException但它没有详细属性。我怎样才能了解这个例外的细节?

回答

0

玩这个很长一段时间,我发现离FaultException异常的对象后,您可以创建一个MessageFault。 MessageFault具有HasDetail属性,用于指示是否存在详细对象。从那里你可以抓取Detail对象作为XmlElement并获取它的值。下面的catch块效果很好。

catch (System.ServiceModel.FaultException FaultEx) 
    { 
    //Gets the Detail Element in the 
    string ErrorMessage; 
    System.ServiceModel.Channels.MessageFault mfault = FaultEx.CreateMessageFault(); 
    if (mfault.HasDetail) 
    ErrorMessage = mfault.GetDetail<System.Xml.XmlElement>().InnerText; 
    } 

这会产生“无效的ID”。来自问题中的样本错误。

-2

使用调用Web服务围绕一个try catch块,再搭上肥皂例外

catch (SoapException e) 
{ 
    e.Detail 
} 

,如果你想抛出非基本FaultExceptions(即包含细节的),你将需要此行为添加到您的web.config,它通过使用behaviorConfiguration属性附加到service

<serviceBehaviors> 
    <behavior name="YourServiceNameOrAnythingReallyServiceBehavior"> 
     <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="true" /> 
    </behavior> 
    </serviceBehaviors> 

然后你想抛出一个new FaultException<T>(T)其中T是包含细节的对象的类型。然后你可以在外面捕获它作为FaultException<T>并以这种方式查看细节。 T可以是一个复杂的类型,如果是的话,你必须装点该类型与[DataContractAttribute]

+0

“在C#中我能赶上它作为一个的FaultException但它没有详细属性。” – Gratzy

+0

你在调用旧时间的Web服务,还是你的WCF调用另一个WCF?你在WCF服务中捕获异常,还是在调用程序中捕获异常? – ohmusama

+0

它的所有问题我打电话给一个asmx服务我在wcf服务中捕获它 – Gratzy