简体   繁体   English

来自Asmx Web服务的WCF客户端捕获SoapException

[英]WCF Client Catching SoapException from asmx webservice

I have a WCF service that calls an asmx web service. 我有一个WMF服务,该服务调用asmx Web服务。 That web service throws an enxception that looks like this: 该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>

In c# I can catch it as a FaultException however it has no details property. 在c#中,我可以将其捕获为FaultException,但是它没有details属性。 How can I get at the details of this Exception? 我如何获得此异常的详细信息?

After playing around with this for a long time I found that off of the FaultException object you can create a MessageFault. 玩了很长时间后,我发现可以从FaultException对象中创建一个MessageFault。 MessageFault has a property HasDetail that indicates if the detail object is present. MessageFault具有HasDetail属性,该属性指示明细对象是否存在。 From there you can grab the Detail object as an XmlElement and get its value. 从那里可以将Detail对象作为XmlElement获取,并获取其值。 The following catch block works well. 以下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;
  } 

This yields "Invalid ID." 这将产生“无效的ID”。 from the sample fault in the question. 从问题中的样本错误。

use a try catch block around the call to the web service, and then catch the soap exception 在对Web服务的调用周围使用try catch块,然后捕获soap异常

catch (SoapException e)
{
    e.Detail
}

if you want to throw non-basic FaultExceptions, (ie ones that contain details) you will need to add this behavior to your web.config and attach it to your service by using the behaviorConfiguration attribute. 如果要抛出非基本的FaultException,即包含详细信息的FaultException,则需要将此行为添加到web.config中,并使用behaviorConfiguration属性将其附加到service

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

Then you would want to throw a new FaultException<T>(T) where T is the type of the object that contains the details. 然后,您将要抛出new FaultException<T>(T) ,其中T是包含详细信息的对象的类型。 You can then catch it on the outside as FaultException<T> and view the details that way. 然后,您可以将其作为FaultException<T>从外部捕获,并以这种方式查看详细信息。 T may be a complex type, if so, you must decorate that type with the [DataContractAttribute] T可能是复杂类型,如果是这样,则必须使用[DataContractAttribute]装饰该类型

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

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