使用VB.NET 4.5.1客户端

时间:2015-05-12 14:35:24

标签: java vb.net web-services wcf soap

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

我添加了服务“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查询了web服务,我已将其作为webservice的响应捕获:

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

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

有谁知道怎么做?

更新:我注意到我的服务网址错误(它引用了导入的部分)。更正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,我捕获了对Web服务的传出请求和返回值。然后,我使用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 ui中编辑并运行.net查询: 请注意从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服务之前修改传出查询,以删除命名空间?

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

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

但是,有更好的方法吗?

1 个答案:

答案 0 :(得分:2)

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

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