WCF接口的COM调用

服务接口往往会被跨平台、跨技术调用。使用非.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接口需要被跨技术调用, 请一定要使用简间数据类型的接口参数和返回值。

posted @ 2011-04-11 10:12  快乐的老毛驴  阅读(672)  评论(1编辑  收藏  举报