【问题标题】:Invalid request to SOAP service does not produce expected SOAP fault对 SOAP 服务的无效请求不会产生预期的 SOAP 错误
【发布时间】:2018-12-14 07:33:11
【问题描述】:

我正在调用 WildFly (JBoss) 上运行的 SOAP 服务,其中包括此操作:

<wsdl:operation name="checkIn">
  <wsdl:documentation>blah</wsdl:documentation>
  <wsdl:input message="tns:checkInRequestMsg" />
  <wsdl:output message="tns:checkInResponseMsg" />
  <wsdl:fault name="error" message="tns:errorMsg" />
</wsdl:operation>

当我在 Fiddler 中使用无效请求调用此服务时,我收到一个 HTTP 500 响应,基本上看起来像这样(我已经稍微编辑了):

<?xml version="1.0" encoding="utf-16"?>
<SOAP:Envelope xmlns:tns="..." xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP:Body>
      <tns:error version="1.0">
         <reason>...</reason>
      </tns:error>
   </SOAP:Body>
</SOAP:Envelope>

当我使用 Visual Studio 2015 生成的 .NET WCF 服务代理使用相同的无效请求调用此服务时,我没有收到 .NET 异常并且响应为空。我无法解析错误响应。

代理以这种方式定义操作:

    // CODEGEN: Generating message contract since the operation checkIn is neither RPC nor document wrapped.
    [System.ServiceModel.OperationContractAttribute(Action="urn:checkIn", ReplyAction="*")]
    [System.ServiceModel.FaultContractAttribute(typeof(ConsoleApplication2.ServiceReference1.ServiceError), Action="urn:checkIn", Name="error")]
    [System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
    ConsoleApplication2.ServiceReference1.checkInResponse checkIn(ConsoleApplication2.ServiceReference1.checkInRequest request);

如何在 .NET 中调用该服务,以便接收解析的错误响应而不是获取空响应?我希望能够使用 .NET WCF 生成的代理,并避免解析原始 XML 响应!

【问题讨论】:

  • 如果您使用添加服务参考或 svcutil.exe 来生成操作合同和数据合同,wcf 很可能正在重命名某些元素以及添加或预期包装标记。您可能需要调整 .net 生成的代码的属性。我建议为消息添加诊断跟踪,以便您可以看到来回发送的内容。您的响应可能不为空,但在由客户端通道方法处理时不会被反序列化。

标签: .net wcf soap jboss wildfly


【解决方案1】:

我编写代码如下,可以捕获自定义异常。

我的服务合同。

 [ServiceContract]
public  interface Test
{
    [OperationContract]
    [FaultContract(typeof(CalculatorFault))]
    [XmlSerializerFormat(SupportFaults = true)]
    double add(double a, double b);
}

我的异常模型。

 [DataContract]
public class CalculatorFault
{
    [DataMember]
    public string OperationName { get; set; }
    [DataMember]
    public string Fault { get; set; }
}

我的服务。

 public class ServiceTest : Test
{

    public double add(double a, double b)
    {

        if (b == 0)
        {
            throw new FaultException<CalculatorFault>(new CalculatorFault { OperationName = "divide", Fault = "hello" },"message");
        }
        return a + b;
    }
}

我的客户代码。

  static void Main(string[] args)
    {
        using (ChannelFactory<Test> testf = new ChannelFactory<Test>("test"))
        {
         Test test =   testf.CreateChannel();
            try
            {
                test.add(0, 0);
            }
            catch (FaultException<CalculatorFault> ex)
            {


            }    
        }
    }

【讨论】:

  • 感谢您的帮助。不幸的是,我不控制服务器端,它不是 .NET,它不会引发正确的 SOAP 错误,尽管 WSDL 说应该这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-22
  • 2015-04-11
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
  • 1970-01-01
  • 2014-06-18
相关资源
最近更新 更多