服務介面往往會被跨平台、跨技術調用。使用非.NET技術(如asp, vb6)調用WCF服務介面時只能使用COM方式,通常有三種方法
1、使用SOAP SDK中的Soap對象
2、使用HTTPRequest對象
3、使用WCF的COM介面
其中第三種調用相對簡單,個人推薦使用。下面講一下第三種調用方式
為了滿足COM對象調用,.NET Framework3.5SP1把WCF代理介面註冊了COM組件對象,它就是Moniker對象,我們可以使用OLE Com Viewer工具來找到它
擷取這個對象可以使用GetObject方法。如下面的ASP代碼示範了如何擷取Moniker對象,並調用它
1 <%
2 Response.write "Coding started!"
3 Response.write"</br>"
4
5 Dim mexMonikerString
6 Dim mexServiceMoniker
7
8 mexMonikerString = "service:mexAddress=http://xxxxx.aaaa.com:81/WcfService1/Service1.svc?wsdl"
9 mexMonikerString = mexMonikerString + ", address=http://xxxx.aaaa.com:81/WcfService1/Service1.svc/"
10 mexMonikerString = mexMonikerString + ", binding=WSHttpBinding_IService1,bindingNamespace=http://tempuri.org/"
11 mexMonikerString = mexMonikerString + ", contract=IService1, contractNamespace=http://tempuri.org/"
12
13 Set mexServiceMoniker=GetObject(mexMonikerString)
14
15 mexServiceMoniker.ChannelCredentials.SetUserNameCredential "WCFCaller", "WCFCaller"
16 Response.write mexServiceMoniker.Echo("sss")
17
18
19 Set mexServiceMoniker = Nothing
20 %>
調用GetObject方法時使用的參數包含了:
1、中繼資料交換地址。
指的是WCF介面的WSDL檔案地址
2、服務地址
指的是WCF介面的服務地址,即svc檔案的URI。
3、綁定名
WSDL檔案中的 wsdl:binding 配置節, name 屬性
4、服務契約名
WSDL檔案中的 wsdl:portType 配置節, name屬性
如果以上配置都正確,調用時還是出錯的話,很可能是你的服務介面中包含了物件類型的參數或傳回值,這些資料無法在非.net環境中被封裝。所以如果你的WCF介面需要被跨技術調用, 請一定要使用簡間資料類型的介面參數和傳回值。