kowalikw
kowalikw

Reputation: 11

Get details from Bad Request (400) response using ClientBase in C#

I have a problem with getting details from SOAP service response. The SOAP service returns some error codes using status 400 (BadRequest). When I call the service using SOAP UI, I see the error details like below:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <s:Fault>
         <faultcode>s:Client</faultcode>
         <faultstring xml:lang="en-US">The creator of this fault did not specify a Reason.</faultstring>
         <detail>
            <Document xmlns="http://schemas.datacontract.org/2004/07/BM.Services.WebservicesOrchestrator.Domain.Errors.ErrorResponse" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
               <errRptField>
                  <errDescField>
                     <ValidationResult3>
                        <elmtField>
                           <ElementIdentification3>
                              <elmtNmField>SchCrit</elmtNmField>
                              <elmtPthField>GetAcct.AcctQryDef.AcctCrit.Item.SchCrit</elmtPthField>
                              <elmtValField i:nil="true"/>
                           </ElementIdentification3>
                        </elmtField>
                        <ruleDescField>Invalid account format</ruleDescField>
                        <ruleIdField>InvalidAccountFormat</ruleIdField>
                        <seqNbField>ee45add9-cc32-4108-8245-192fab5f574e</seqNbField>
                     </ValidationResult3>
                  </errDescField>
                  <estblishdBaselnIdField i:nil="true"/>
                  <nbOfErrsField>
                     <nbField>1</nbField>
                  </nbOfErrsField>
                  <reqForActnField i:nil="true"/>
                  <rjctdMsgRefField i:nil="true"/>
                  <rptIdField>
                     <creDtTmField>2021-09-15T11:54:24.8168124+02:00</creDtTmField>
                     <idField i:nil="true"/>
                  </rptIdField>
                  <txIdField i:nil="true"/>
                  <txStsField i:nil="true"/>
                  <usrTxRefField i:nil="true"/>
               </errRptField>
            </Document>
         </detail>
      </s:Fault>
   </s:Body>
</s:Envelope>

But when I try to call service from code using client generated in Visual Studio as AccountsClient : System.ServiceModel.ClientBase I only receive System.ServiceModelProtocolException with message without any other details:

The remote server returned an unexpected response: (400) Bad Request.

What sould I change to receive some other details with Bad Request (http 400 status) using ClientBase derieved class to call SOAP service? I've read about HttpWebRequest but I don't want to change the way I call SOAP service.

Thank you for your help!

Upvotes: 1

Views: 786

Answers (1)

framerelay
framerelay

Reputation: 686

In your C# code try to catch System.ServiceModel.FaultException where T according to what I see from SOAP UI response should be ErrorResponse type (Document xmlns="http://schemas.datacontract.org/2004/07/BM.Services.WebservicesOrchestrator.Domain.Errors.ErrorResponse"). So the code must be as follow

try
{
  var response = soapClient.CallYourFunction();
}
catch(FaultException<ErrorResponse> fault)
{
 // here is typed fault with details usually in the field fault.Detail according to your model
}
catch (FaultException fault)
{
 // here is the general fault
}
catch (Exception fault)
{
 // something else but still general exception, usually network layer exceptions
}

Upvotes: 0

Related Questions