第一步:创建WCF服务1 打开Visual Studio 2008, 新建一项目,我们这里建立一个Web站点,采用WCF Service Application模板,新项目名称为WCFServiceMoniker.
2 单击“Ok”之后,VS2008将为我们生成相应的目录结构和文件.
3 为了简单,我们不对生成的目录结构做任何改动。接下来,我们打开IService1.cs文件,删除系统生成的操作契约,然后增加我们自己的契约如下:
namespace WCFServiceMoniker { [ServiceContract] public interface IService1 { [OperationContract] string SayHello(string yourwords); } }
4 接着我们打开Service1.cs文件,删除自动生成的内容,增加对SayHello方法的实现:
namespace WCFServiceMoniker { public class Service1 : IService1 { public string SayHello(string yourwords) { return string.Format("Hello World! You ntered: {0}", yourwords); } } }
5 针对Web.config我们不做任何改动,其关于WCF部分的设置如下:
<system.serviceModel> <services> <service name="WCFServiceMoniker.Service1" behaviorConfiguration="WCFServiceMoniker.Service1Behavior"> <!-- Service Endpoints --> <endpoint address="" binding="wsHttpBinding" contract="WCFServiceMoniker.IService1"> <!-- Upon deployment, the following identity element should be removed or replaced to reflect the identity under which the deployed service runs. If removed, WCF will infer an appropriate identity automatically. --> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="WCFServiceMoniker.Service1Behavior"> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
6 好了,我们的WCF服务已经建好了,编译一下,一切Ok. 接下来我们需要在IIS中创建一个虚拟目录了,我们叫WCFServiceMoniker,让其指向WCF服务所在的目录。7 好了,我们在IE中输入http://localhost/WCFServiceMoniker/Service1.svc看看,是否能看到我们熟悉的WCF Service的界面,正常情况下应该没有问题。
第二步:创建客户端库文件1 在解决方案中增加Client项目。这里我们选中的是Class Library模板,项目名称为Client.
2 单击”Ok”按钮之后,VS2008将为我们生成一个包含Class1.cs文件的项目,我们先删除Class1.cs文件,然后打开Client的项目属性页。在Build Events的Pre-build event command line中增加产生WCF Service客户端的命令:Call "D:/Program Files/Microsoft Visual Studio 9.0/VC/vcvarsall.bat" x86svcutil /language:cs /config:$(ProjectDir)App.config /out:$(ProjectDir)generatedClient.cs http://localhost/WCFServiceMoniker/service1.svc?wsdl
注意:在这里我的VS2008是安装在D盘,如果你的VS2008安装在其他位置,你需要将第一个命令行改成相应的安装目录。
4 打开Client项目的AssemblyInfo.cs文件,然后找到[assembly: ComVisible(false)],将其改成[assembly: ComVisible(true)],更改这个属性的目的是为了后面将类型库注册成COM。5 再次打开Client的项目属性,选中Signing(签名)标签,然后在sign the assembly前面的Checkbox打上勾。这时下面的Choose a strong name key file将从disable变成enable,我们选择<New…>,然后将弹出一个创建强命名Key文件的对话框,我们输入 WCFServiceMonikerKey,然后输入你所希望的密码,例如:password,然后点击“OK”我们的Key文件就创建好了。(这一步主要是为了将生成的程序集放入全局程序集缓存中。)
6 再次编译一下Client,发现系统找不到ServiceModel命名空间,我们将System.ServiceModel添加到Reference(应用)中,再次编译,一切搞定。
第三步:注册Client程序集1 将Client.dll添加的全局程序集缓存中 Gacutil /i Client.dll2 向COM注册Client.dll程序集中的类型 Regasm Client.dll /t运行此命令之后,将会注册程序集中的类型,同时生成Client.tlb文件。
3 现在如果我们打开OLEView,我们就可以在Type Libraries(类型库)中看到刚才注册的Client类型了。4 由于我们可能需要多次进行注册反注册工作,所以我为此写了两个批处理文件。以下是这两个批处理文件的内容:注册的批处理文件Reg.bat的内容如下:
@echo off"D:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/Bin/gacutil" /i Client.dll %systemroot%/Microsoft.NET/Framework/v2.0.50727/Regasm Client.dll /tPause
反注册的批处理文件UnReg.bat的内容如下:
@echo off"D:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/Bin/gacutil" /u Client%systemroot%/Microsoft.NET/Framework/v2.0.50727/Regasm Client.dll /t /urem del ./bin/debug/Client.dlldel Client.tlbPause
第四步:在Script中调用WCF服务1 在我们写Script文件之前,首先让我们用OLE打开刚才注册的Client.dll的COM类型信息看看.
2 大家注意红色框框中的内容,左边红框中是我们的服务契约IService1接口,右边红框是注册之后的UUID,在我们的Script中将要使用这个UUID.3 下面就是创建我们的Script文件了,我们起名为CallWCFService_TypedContract.vbs,内容如下:
'--------------------------------------------------------------- ' Typed Contract service moniker example '--------------------------------------------------------------- ' Create a service moniker object using a strongly typed contract ' This references the address, a standard binding type and the ' locally registered COM-visible contract ID monikerString = "service:address='http://localhost/WCFServiceMoniker/Service1.svc'" monikerString = monikerString + ", binding=wsHttpBinding" monikerString = monikerString + ", contract={4FBDA94E-8B89-32EC-BC28-2A0A5E9B7C74}" ' Create the service moniker object Set serviceMoniker = GetObject(monikerString) ' Call the service operations using the moniker object WScript.Echo serviceMoniker.SayHello("I am LazyBee, My blog is [url]http://lazybee.cnblogs.com/[/url] ") Set serviceMoniker= nothing
4 保存之后,直接双击运行,你就可以看到运行结果了:
第五步:在Word的宏中调用WCF服务1 打开Word, 添加一个宏(宏-)Visal Basic编辑器或者通过录制宏的方式添加),增加下面的代码:
Sub CallWCFService_TypedContract()Sub CallWCFService_TypedContract() Dim strMonikerString As String Dim serviceMoniker As Object '--------------------------------------------------------------- ' Typed Contract service moniker example '--------------------------------------------------------------- ' Create a service moniker object using a strongly typed contract ' This references the address, a standard binding type and the ' locally registered COM-visible contract ID monikerString = "service:address='http://localhost/WCFServiceMoniker/Service1.svc'" monikerString = monikerString + ", binding=wsHttpBinding" monikerString = monikerString + ", contract={4FBDA94E-8B89-32EC-BC28-2A0A5E9B7C74}" ' Create the service moniker object Set serviceMoniker = GetObject(monikerString) ' Call the service operations using the moniker object MsgBox serviceMoniker.SayHello("I am LazyBee, My blog is [url]http://lazybee.cnblogs.com/[/url] ") Set serviceMoniker = Nothing End Sub
2 直接运行,同样能得到上图类似的结果。注:在VB6中调用和Word中调用是一样的。