0

I'm trying to catch the FaultException on a WCF client.

the generated reference contract is:

        [System.ServiceModel.OperationContractAttribute(Action="urn:wcfname#method", ReplyAction="*")]
        [System.ServiceModel.FaultContractAttribute(typeof(Error[]), Action= "urn:wcfname#method", Name="errors")]
        [System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
        [System.ServiceModel.ServiceKnownTypeAttribute(typeof(Response))]
        [System.ServiceModel.ServiceKnownTypeAttribute(typeof(Request))]
        service.response method(service.request request);

and the generated error class is:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="wcf")]
    public partial class Error
    {
        
        private string errorCodeField;
        
        private string errorStringField;
        
        private string subErrorCodeField;
        
        private string offendingFieldField;
        
        private System.DateTime timeStampField;
        
        private bool timeStampFieldSpecified;
        
        private string detailStringField;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(DataType="integer", Order=0)]
        public string errorCode
        {
            get
            {
                return this.errorCodeField;
            }
            set
            {
                this.errorCodeField = value;
            }
        }
        
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order=1)]
        public string errorString
        {
            get
            {
                return this.errorStringField;
            }
            set
            {
                this.errorStringField = value;
            }
        }
        
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(DataType="integer", Order=2)]
        public string subErrorCode
        {
            get
            {
                return this.subErrorCodeField;
            }
            set
            {
                this.subErrorCodeField = value;
            }
        }
        
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order=3)]
        public string offendingField
        {
            get
            {
                return this.offendingFieldField;
            }
            set
            {
                this.offendingFieldField = value;
            }
        }
        
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order=4)]
        public System.DateTime timeStamp
        {
            get
            {
                return this.timeStampField;
            }
            set
            {
                this.timeStampField = value;
            }
        }
        
        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool timeStampSpecified
        {
            get
            {
                return this.timeStampFieldSpecified;
            }
            set
            {
                this.timeStampFieldSpecified = value;
            }
        }
        
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order=5)]
        public string detailString
        {
            get
            {
                return this.detailStringField;
            }
            set
            {
                this.detailStringField = value;
            }
        }
    }

the code to call the sercie is below:

try
{
   //call the service method
}
catch (FaultException<Error[]> ex)
{
   var errorElement = XElement.Parse(ex.CreateMessageFault().GetReaderAtDetailContents().ReadOuterXml());
   var xmlDetail = (string)errorElement;
}
catch (FaultException ex)
{
   var errorElement = XElement.Parse(ex.CreateMessageFault().GetReaderAtDetailContents().ReadOuterXml());
   var xmlDetail = (string)errorElement;

}

When theres a FaultExeption<Error[]> ex, it enters there, but the error is empty string. If I comment that part, it enters on the FaultException ex, but again the error is empty string.

I'm using .netCore 3.1

desertnaut
  • 57,590
  • 26
  • 140
  • 166
rahebirizah
  • 115
  • 11

2 Answers2

0

Throwing FaultException indicates that there is no problem with the channel. The exception was thrown by the service. WCF's FaultException message is too general to pinpoint a specific problem, so you can customize the exception message like this:

public class HomeService : IHomeService
      {
          public Student Get(string id)
          {
              try
              {
                  //Here, of course, an exception will be thrown
                 var result = Convert.ToInt32(id) / Convert.ToInt32("0");
 
         return new Student() { ID = Convert.ToInt32(id), Name = "hxc", SNS = "001" };
             }
             catch (Exception ex)
             {
                 var reason = new FaultReason("Exception thrown information");
 
                 var code = new FaultCode("500");
 
                 var faultException = new FaultException(reason, code, "It is GET that throws the exception");
 
                 throw faultException;
             }
         }
     }
Jiayao
  • 510
  • 3
  • 7
  • Hi Jiayao, thanks for the feedback. I don't manage the service, I just consume it. The problem is that I'm not able to read the exception message that is thrown by the Service – rahebirizah Aug 23 '21 at 07:32
-1

The reason must be there are multiple namespaces in the response.

see the link for a workaround: multiplenamespacesres

semre
  • 1
  • 2