COM(VB/VBA/Script)利用服务标记调用WCF服务
之二使用MEX契约
LazyBee(http://lazybee.cnblogs.com)
上一篇我们使用类型化契约的服务标记在COM中调用WCF服务,不过这个有一定的局限性,因为需要注册类型,所以需要.Net FrameWork,但是在很多情况下COM客户端所在的环境没有.Net Framework,比如说在Excel中,需要调用一个WCF服务来计算某个单元格的值,而且这个Excel文件可能分发到许多机器上,你总不能给每台机器都装一个.Net Framework,同时都去给它注册所需的类型吧。针对这种场景,我们可以使用MEX契约的服务标记来动态得到服务契约,这样就可以满足我们上面的场景的要求了。下面就让我们开始吧!
第一步:创建WCF服务
这个和上一篇的完全一样,具体请参见COM(VB/VBA/Script)利用服务标记调用WCF服务之一使用类型化契约的第一步,如果您曾经按照上一篇文章做过相应的测试,你就可以直接使用已经建好的服务了。
第二步:在Script中调用WCF服务
1 这里大家对比前面一篇就可以看到,这里没有任何创建客户端库以及注册等步骤,而是直接进行调用WCF服务了。在写调用代码之前,首先让我们打开服务的WSDL来看看,因为这里有我们需要的服务标记信息。
2 我们打开http://localhost/WCFServiceMoniker/Service1.svc?wsdl之后,如下图所示:
![](https://i-blog.csdnimg.cn/blog_migrate/b7f6ee5a40573d31d198d444cdf730b4.jpeg)
3 下面就是创建我们的Script文件了,我们起名为CallWCFService_mex.vbs,内容如下:
1
'
---------------------------------------------------------------
2
'
MEX service moniker example
3
'
---------------------------------------------------------------
4
'
Create a string for the service moniker specifying the address
5
'
to retrieve the service metadata from
6
mexMonikerString
=
"
service:mexAddress='http://localhost/WCFServiceMoniker/Service1.svc/mex'
"
7
mexMonikerString
=
mexMonikerString
+
"
, address='http://localhost/WCFServiceMoniker/Service1.svc'
"
8
mexMonikerString
=
mexMonikerString
+
"
,binding=WSHttpBinding_IService1, bindingNamespace='http://tempuri.org/'
"
9
mexMonikerString
=
mexMonikerString
+
"
, contract=IService1, contractNamespace='http://tempuri.org/'
"
10
'
Create the service moniker object
11
Set
mexServiceMoniker
=
GetObject
(mexMonikerString)
12
'
Call the service operations using the moniker object
13
WScript.Echo mexServiceMoniker.SayHello(
"
I am LazyBee, My blog is http://lazybee.cnblogs.com/
"
)
14
Set
mexServiceMoniker
=
nothing
15
注意:在我们服务标记字符串中,包含
Mex
地址、服务地址、绑定及命名空间、服务契约及命名空间四部分内容。在这里服务地址就是上图所标注的地址,当然
Mex
地址就是在其后面增加一个
mex
了,这个对大家来说应该都很好理解。绑定及服务也是上图所标注,不过注意其大小写,因为在服务标记中是区分大小写的,由于我们没有给服务契约和绑定指定命名空间,缺省就是“
http://tempuri.org/
”了。
4 保存之后,直接双击运行,你就可以看到运行结果了:
第三步:在VBA的宏中调用WCF服务
1 在VBA中的代码和上一篇文章COM(VB/VBA/Script)利用服务标记调用WCF服务之一使用类型化契约的一样,只是将服务标记字符串和上面一样做相应的更改就可以了,如下所示:
2 直接运行,同样能得到上图类似的结果。
Code
1![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Sub CallWCFService_Mex()Sub CallWCFService_Mex()
2
Dim strMonikerString As String
3
Dim serviceMoniker As Object
4
'---------------------------------------------------------------
5
' MEX service moniker example
6
'---------------------------------------------------------------
7
' Create a string for the service moniker specifying the address
8
' to retrieve the service metadata from
9
mexMonikerString = "service:mexAddress='http://localhost/WCFServiceMoniker/Service1.svc/mex'"
10
mexMonikerString = mexMonikerString + ", address='http://localhost/WCFServiceMoniker/Service1.svc'"
11
mexMonikerString = mexMonikerString + ", binding=WSHttpBinding_IService1, bindingNamespace='http://tempuri.org/'"
12
mexMonikerString = mexMonikerString + ", contract=IService1, contractNamespace='http://tempuri.org/'"
13
' Create the service moniker object
14
Set mexServiceMoniker = GetObject(mexMonikerString)
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
' Call the service operations using the moniker object
17
MsgBox mexServiceMoniker.SayHello("I am LazyBee, My blog is http://lazybee.cnblogs.com/ ")
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
Set mexServiceMoniker = Nothing
20
End Sub
21![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
注:在VB6中调用和上面的代码一样。
本文的源代码下载地址为:http://files.cnblogs.com/LazyBee/WCFServiceMoniker_MEX.Zip
(转载请注明出处:http://lazybee.cnblogs.com)