0

我从 SOAP 服务响应中获取详细信息时遇到问题。SOAP 服务使用状态 400 (BadRequest) 返回一些错误代码。当我使用 SOAP UI 调用服务时,我看到如下错误详细信息:

<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>

但是,当我尝试使用 Visual Studio 中生成的客户端作为 AccountsClient 从代码调用服务时: System.ServiceModel.ClientBase 我只收到 带有消息的System.ServiceModelProtocolException没有任何其他详细信息:

远程服务器返回意外响应:(400) 错误请求。

使用 ClientBase 派生类调用 SOAP 服务,我应该改变什么来接收带有错误请求(http 400 状态)的其他详细信息?我读过 HttpWebRequest 但我不想改变调用 SOAP 服务的方式。

谢谢您的帮助!

4

1 回答 1

0

在您的 C# 代码中尝试捕获 System.ServiceModel.FaultException 其中 T 根据我从 SOAP UI 响应中看到的应该是 ErrorResponse 类型(文档 xmlns="http://schemas.datacontract.org/2004/07/BM.Services. WebservicesOrchestrator.Domain.Errors.ErrorResponse”)。所以代码必须如下

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
}
于 2021-09-15T19:41:43.463 回答