【问题标题】:Calling WCF service by VBScript通过 VBScript 调用 WCF 服务
【发布时间】:2010-10-31 00:09:40
【问题描述】:

有一个带有配置的 WCF 服务:

<services>
  <service name="MyService" behaviorConfiguration="MyServiceBehavior">
    <endpoint 
      binding="basicHttpBinding"  
      contract="IMyService" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8001/MyService" />
      </baseAddresses>
    </host>
  </service>
</services>

<behaviors>
  <serviceBehaviors>
    <behavior name="MyServiceBehavior">
      <serviceMetadata httpGetEnabled="True" />
    </behavior>
  </serviceBehaviors>
</behaviors>

这个脚本应该调用它:

Option Explicit

Dim soapClient
Dim serviceUri
Dim serviceName
Dim portName
Dim result

serviceUri = "http://localhost:8001/MyService"
serviceName = "MyService"
portName = "BasicHttpBinding_IMyService"

Set soapClient = CreateObject("MSSOAP.soapClient")
soapClient.ClientProperty("ServerHTTPRequest") = True
soapClient.mssoapinit serviceUri & "?WSDL", serviceName, portName

运行脚本时出现此错误:

客户端:WSDLReader:分析 WSDL 文件失败 HRESULT=0x8 0004005 - WSDLReader:服务初始化失败 HRESULT=0x80004005 - WSDL 服务:服务 MyService 的端口初始化失败 HRESULT =0x80004005 - WSDLPort:分析端口 BasicHttpBinding_IMyService 的绑定信息失败 HRESULT=0x80004005 - WSDLPort:无法初始化端口 BasicHttpBinding_IMyService 的操作 HRESULT=0x8000 4005 - WSDLOperation:操作 //def:portType[@name="IMyService"]/ def:operation[@name="MyMethod"] 在 porttype 部分中找不到 HRESULT=0x80004005

出了什么问题?请帮忙。

编辑:

感谢 Cheeso 的回答。 MSSOAP 的问题似乎在于它要求所有 xsd 模式都内嵌在生成的 WSDL 文件中。 WCF 默认不这样做。

【问题讨论】:

  • 如果您想要 WSDL 中的 XSD,您可以修改 WCF 服务以“扁平化”它。 bing.com/search?q=Wcf+flatten+wsdl 如果您无法控制 WCF 端,您仍然可以构建一个整体的并且应该可以工作的 WSDL。但 MSSOAP 还存在其他更严重的问题。

标签: wcf vbscript msxml


【解决方案1】:

不要使用 MSSOAP。我认为在过去的 3 或 4 年里,它现在已经不支持了。考虑使用 XmlHttp,它是 MSXML 的一部分,受支持并将继续维护。您将不得不手动构建一个 SOAP 信封。但这种方式更可靠。

示例代码

' URL to the WCF service'
url= "http://server:port/Wcf.Service.Address"

Dim requestDoc
Set requestDoc = WScript.CreateObject("MSXML2.DOMDocument.6.0")

Dim root
Set root = requestDoc.createNode(1, "Envelope", "http://schemas.xmlsoap.org/soap/envelope/")
requestDoc.appendChild root

Dim nodeBody
Set nodeBody = requestDoc.createNode(1, "Body", "http://schemas.xmlsoap.org/soap/envelope/")
root.appendChild nodeBody

Dim nodeOp
Set nodeOp = requestDoc.createNode(1, "Register", "urn:Your.Namespace.Here")
nodeBody.appendChild nodeOp

Dim nodeRequest
Set nodeRequest = requestDoc.createNode(1, "request", "urn:Your.Namespace.Here")
'content of the request will vary depending on the WCF Service.'
' This one takes just a plain string. '
nodeRequest.text = "Hello from a VBScript client."

nodeOp.appendChild nodeRequest

Set nodeRequest = Nothing
Set nodeOp = Nothing
Set nodeBody = Nothing
Set root = Nothing


'the request will look like this:'
'       <s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'> '
'         <s:Body> '
'           <Register xmlns='urn:Your.Namespace.Here'> '
'               <request>hello from a VBScript client.</request> '
'           </Register> '
'         </s:Body> '
'       </s:Envelope>'


WSCript.Echo  "sending request " & vbcrlf & requestDoc.xml


dim xmlhttp

set xmlhttp = WScript.CreateObject("MSXML2.ServerXMLHTTP.6.0")
' set the proxy as necessary and desired '
xmlhttp.setProxy 2, "http://localhost:8888"
xmlhttp.Open "POST", url, False
xmlhttp.setRequestHeader "Content-Type", "text/xml"
' set SOAPAction as appropriate for the operation '
xmlhttp.setRequestHeader "SOAPAction", "urn:Set.As.Appropriate"
xmlhttp.send requestDoc.xml

WScript.Echo vbcrlf & "Raw XML response:" & vbcrlf 
WSCript.Echo  xmlhttp.responseXML.xml

dim response
set response= xmlhttp.responseXML
'the response is an MSXML2.DOMDocument.6.0' 
'party on the response here - XPath, walk the DOM, etc. '

仅供参考:请参阅which-version-of-msxml-should-i-use 了解如何选择 MSXML 版本。

【讨论】:

  • @Cheeso:即使在 VBScript 中,也不应该通过字符串连接来创建 XML。它应该通过 MSXML 创建,然后应该发送 .xml 属性。
  • 好点,完全同意。正确的做法是通过 DOM 创建一个文档。在这种情况下,我使用了一个 quick-n-dirty 字符串来显示线路上发生了什么。
  • @Cheeso:很好,但是人们会复制这些答案并按原样使用它们。我们必须小心不要教坏习惯。 OTOH,您可以在字符串 concat 之前添加“不要在家尝试 - 改用 MSXML”注释...
  • 好吧,乔恩,你说服了我。我更新了示例以使用 DOM。
  • @Cheeso:关于坏习惯的问题,另一个人使用 4.0 而不是 3.0 或 6.0。在这种情况下,我会使用 3.0,它可以在当前支持的任何 Windows 版本上运行。
猜你喜欢
  • 2011-11-21
  • 2010-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多