使用 VB.NET 4.5.1 客户端连接到基于 Java 的 Web 服务时出现解组错误

Unmarshalling error when connecting to Java-based webservice using VB.NET 4.5.1 client

我正在尝试构建一个 Web 应用程序,它将读取数据 to/from 一个数据库并查询 post 数据到远程 Web 服务。 Web 应用程序是在 .NET Framework 4.5.1 上用 vb.net 编写的。我正在为 IDE.

使用 VS2013

我添加了服务“https://service.provider.com/..../ws/soap?wsdl=SOAP.wsdl”作为服务参考,并编写了以下内容来查询该服务:

Dim client As Service1.SOAPClient = New Service1.SOAPClient("Service1Soap")

Dim session = New Service1.session With {.id = 0, .login = "username", .password = "password"}

Dim response = client.openSession(session)              

If response.status = Service1.responseStatus.success Then
    'yay
ElseIf response.status = Service1.responseStatus.failure Then
    'boo
End If

web.config 中的 service.serviceModel 部分不是自动生成的,所以我不得不根据每次 运行 时收到的各种错误手动拼凑它。我最终想出了以下内容:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="Service1Soap">
          <security mode="Transport" />
        </binding>
      </basicHttpBinding>
      <customBinding>
        <binding name="Service1Soap12">
          <textMessageEncoding messageVersion="Soap12" />
          <httpTransport />
        </binding>
      </customBinding>
    </bindings>
    <client>
      <endpoint address="https://service.provider.com/.../ws/soap?wsdl=SOAP.wsdl" binding="basicHttpBinding" bindingConfiguration="Service1Soap" contract="Service1.SOAP" name="Service1Soap" />
    </client>
  </system.serviceModel>

现在,当我 运行 应用程序在它尝试调用 openSession 时抛出错误并给我以下错误消息:

Unmarshalling Error: Namespace URIs and local names to the unmarshaller needs to be interned.  
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.ServiceModel.FaultException: Unmarshalling Error: Namespace URIs and local names to the unmarshaller needs to be interned. 

Source Error:  

Line 7771:        <System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)>  _
Line 7772:        Function Service1_SOAP_openSession(ByVal request As Service1.openSession) As Service1.openSessionResponse Implements Service1.SOAP.openSession
Line 7773:            Return MyBase.Channel.openSession(request)
Line 7774:        End Function
Line 7775:        


 Source File:  c:\...\Visual Studio 2013\Projects\WebApplication1\WebApplication1\Service References\Service1\Reference.vb    Line:  7773 

Stack Trace: 



[FaultException: Unmarshalling Error: Namespace URIs and local names to the unmarshaller needs to be interned. ]
   System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +10733331
   System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +336
   WebApplication1.Service1.SOAP.openSession(openSession request) +0
   WebApplication1.Service1.SOAPClient.Service1_SOAP_openSession(openSession request) in c:\...\Projects\WebApplication1\WebApplication1\Service References\Service1\Reference.vb:7773
   WebApplication1.Service1.SOAPClient.openSession(OpenSession openSession1) in c:\...\Visual Studio 2013\Projects\WebApplication1\WebApplication1\Service References\Service1\Reference.vb:7779

...

我已经使用 SoapUI 查询了网络服务,并将其捕获为来自网络服务的响应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns1:openSessionResponse xmlns:ns1="http://autogenerated.service.provider.com/">
         <return xmlns:ns2="http://autogenerated.service.provider.com/">
            <id>0</id>
            <status>success</status>
            <sessionId>8a9b868f4d4650ee014d488308e90243</sessionId>
         </return>
      </ns1:openSessionResponse>
   </soap:Body>
</soap:Envelope>

根据第三方网络服务提供商的说法,此错误与基于 Java 的服务有关,而我使用的是基于 vb.net 的客户端。我显然需要编写一个 WCF 扩展来修改客户端和服务器之间的 SOAP 请求和响应,以删除 SOAP 信封中的名称空间。这是我第一次做.net client-to-java webservice 项目,所以我不知道从哪里开始,服务提供商也无法提供任何更具体的帮助。

有人知道怎么做吗?

更新: 我注意到我的服务 url 是错误的(它引用了导入的部分)。更正 url 后,正确生成的 web.config 如下所示,但我仍然收到与上面相同的解组错误。

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="SOAPSoapBinding">
          <security mode="Transport" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://service.provider.com/.../ws/soap"
          binding="basicHttpBinding" bindingConfiguration="SOAPSoapBinding"
          contract="ServiceReference1.SOAP" name="SOAPImplPort" />
    </client>
  </system.serviceModel>

更新 2: 使用 Fiddler2,我捕获了对网络服务的传出请求和 return 值。然后我使用 SoapUI 将 SoapUI 查询与 .net 生成的查询进行比较并编辑查询直到我得到有效的响应。

.net 生成的查询:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
     <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
          <openSession xmlns="http://autogenerated.service.provider.com/">
               <openSession xmlns="">
                    <id>0</id>
                    <login>username</login>
                    <password>password</password>
               </openSession>
         </openSession>
     </s:Body>
</s:Envelope>

网络服务响应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Client</faultcode>
         <faultstring>Unmarshalling Error: Namespace URIs and local names to the unmarshaller needs to be interned.</faultstring>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

在 soap 中编辑并运行 .net 查询 ui: 注意从 openSession 标记中删除命名空间属性。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
     <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
          <openSession>
               <openSession>
                    <id>0</id>
                    <login>username</login>
                    <password>password</password>
               </openSession>
          </openSession>
     </s:Body>
</s:Envelope>

网络服务响应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns1:openSessionResponse xmlns:ns1="http://autogenerated.service.provider.com/">
         <return xmlns:ns2="http://autogenerated.service.provider.com/">
            <id>0</id>
            <status>success</status>
            <sessionId>8a9b868f4d55c42b014d56f00c990134</sessionId>
         </return>
      </ns1:openSessionResponse>
   </soap:Body>
</soap:Envelope>

所以,现在的问题是:如何在发送到 web 服务之前修改来自客户端应用程序的传出查询,以删除命名空间?

更新三:成功!我通过编辑 Reference.vb 文件并在我需要调用的特定方法上将 WrapperNamespace 和 Namespace 属性设置为“”而不是“http://autogenerated.service.provider.com/”,设法删除了命名空间。然后我将它们复制到同一命名空间内的部分 class 中,并在自动生成的代理 class 中注释掉自动生成的方法。如果代理 class 将来重新生成,那么它将给我一个 build 错误,我只需要再次 remove/comment 它们。客户端现在可以毫无问题地调用网络服务。

自定义Reference.vb:

Namespace ServiceReference1

    <System.Diagnostics.DebuggerStepThroughAttribute(), _
         System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0"), _
         System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced), _
         System.ServiceModel.MessageContractAttribute(WrapperName:="openSession", WrapperNamespace:="", IsWrapped:=True)> _
    Partial Public Class openSession

        <System.ServiceModel.MessageBodyMemberAttribute(Name:="openSession", [Namespace]:="", Order:=0), _
         System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
        Public openSession1 As ServiceReference1.OpenSession

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal openSession1 As ServiceReference1.OpenSession)
            MyBase.New()
            Me.openSession1 = openSession1
        End Sub
    End Class
...
End Namespace

但是,有更好的方法吗?

我通过删除命名空间、编辑 Reference.vb 文件并设置 WrapperNamespaceNamespace 属性,以及我需要的特定方法,设法消除了解组错误调用 "" 而不是 "http://autogenerated.service.provider.com/"。然后我将它们复制到同一命名空间内的部分 class 中,并在自动生成的代理 class 中注释掉自动生成的方法。如果代理 class 将来重新生成,那么它将给我一个构建错误,我只需要再次 remove/comment 它们。客户端现在可以毫无问题地调用网络服务。

CustomReference.vb:

Namespace ServiceReference1

    <System.Diagnostics.DebuggerStepThroughAttribute(), _
         System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0"), _
         System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced), _
         System.ServiceModel.MessageContractAttribute(WrapperName:="openSession", WrapperNamespace:="", IsWrapped:=True)> _
    Partial Public Class openSession

        <System.ServiceModel.MessageBodyMemberAttribute(Name:="openSession", [Namespace]:="", Order:=0), _
         System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
        Public openSession1 As ServiceReference1.OpenSession

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal openSession1 As ServiceReference1.OpenSession)
            MyBase.New()
            Me.openSession1 = openSession1
        End Sub
    End Class
...
End Namespace