2009-06-03 33 views
10

Có một dịch vụ WCF với cấu hình:gọi bằng VBScript

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

Kịch bản này được cho là để gọi nó:

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 

Khi chạy script lỗi này xuất hiện:

Khách hàng: WSDLReader: Phân tích tệp WSDL không thành công HRESULT = 0x8 0004005 - WSDLReader: Khởi tạo dịch vụ không thành công HRESULT = 0x80004005 - Dịch vụ WSDL: Khởi tạo p ort cho dịch vụ MyService không thành công HRESULT = 0x80004005 - WSDLPort: Phân tích thông tin ràng buộc cho cổng BasicHttpBinding_IMyService không thành công HRESULT = 0x80004005 - WSDLPort: Một thao tác cho cổng BasicHttpBinding_IMyService không thể khởi tạo HRESULT = 0x8000 4005 - WSDLOperation: Hoạt động // def: portType [@ name = "IMyService"]/ def: hoạt động [@ name = "MyMethod"] không được tìm thấy trong phần kiểu cổng HRESULT = 0x80004005

Điều gì đang xảy ra? Hãy giúp tôi.

Chỉnh sửa:

Cảm ơn bạn, Cheeso, cho câu trả lời. Vấn đề với MSSOAP dường như là nó yêu cầu tất cả các lược đồ xsd được bao gồm nội tuyến trong tệp WSDL được tạo ra. WCF không thực hiện theo mặc định.

+1

Nếu bạn muốn các XSD trong WSDL, bạn có thể sửa đổi dịch vụ WCF thành "làm phẳng" nó. http://www.bing.com/search?q=Wcf+flatten+wsdl Nếu bạn không có quyền kiểm soát kết thúc WCF, bạn vẫn có thể xây dựng một WSDL đó là nguyên khối và nên hoạt động. Nhưng có những vấn đề khác, nghiêm trọng hơn với MSSOAP. – Cheeso

Trả lời

15

Không sử dụng MSSOAP. Tôi nghĩ rằng bây giờ nó không còn hỗ trợ, trong 3 hoặc 4 năm qua. Hãy xem xét việc sử dụng XmlHttp, một phần của MSXML, và được hỗ trợ và tiếp tục được duy trì. Bạn sẽ phải xây dựng một phong bì SOAP bằng tay. Nhưng nó đáng tin cậy hơn theo cách này.

mã ví dụ

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

FYI: Xem which-version-of-msxml-should-i-use phải học cách để chọn một phiên bản của MSXML.

+0

@Cheeso: ngay cả trong VBScript, XML không nên được tạo thông qua chuỗi nối. Nó sẽ được tạo thông qua MSXML, sau đó thuộc tính .xml sẽ được gửi đi. –

+1

Điểm tốt, Hoàn toàn đồng ý. Điều đúng đắn cần làm là tạo một tài liệu thông qua DOM. Trong trường hợp này, tôi sử dụng một chuỗi nhanh chóng-n-bẩn để chỉ hiển thị những gì đang xảy ra trên dây. – Cheeso

+0

@ Cheeso: Tốt, nhưng mọi người sao chép các câu trả lời này và sử dụng chúng như là. Chúng ta phải cẩn thận không dạy những thói quen xấu. OTOH bạn có thể thêm một "không thử này ở nhà - sử dụng MSXML thay vì" bình luận trước khi chuỗi concat ... –

Các vấn đề liên quan