COM(VB/VBA/Script)利用服务标记调用WCF服务之四:使用配置文件
COM(VB/VBA/Script)利用服务标记调用WCF服务之四:使用配置文件
这几天友人同学说他在COM利用服务标记调用WCF服务的时候遇到一个问题,就是他返回的的Soap消息大于65536这个wsHttpBinding的缺省值,引发调用错误。需要将MaxReceivedMessageSize更改成可以容纳大消息内容的值。在我前面的三篇文章中使用的都是缺省的wsHttpBinding,所以不存在这个问题。现在需要更改缺省值自然就需要增加配置来达到目的。目前最关键的问题就是:是否能让VB/VBA/Script来使用配置文件呢?如果可以,那如何让VB,VBA, Script来使用配置文件中定义的绑定呢? 在多方搜寻探索尝试之后,终于找到了解决方案,那就是只要将配置文件放在宿主文件的相同目录,并且将配置文件的名称改成和宿主文件同名(包括扩展名)再加上.config之后,就可以让VB/VBA/Script来使用配置文件中定义的绑定去调用WCF服务。 LazyBee(http://lazybee.cnblogs.com/)
- 如果是vbs文件(vb script),由于执行vbs文件的是cscript.exe或者wscript.exe,缺省情况下我们系统都是使用wscript.exe来执行的,这时候你可以把配置文件放在系统目录的system32下,名为wscript.exe.config.
- 如果是VBA宏的话,要看是word还是excel,或者其他。因为不同的类型其配置文件名不一样。如果是word,那么配置文件名称应该是WINWORD.EXE.config,如果是excel,配置文件名应该是:Excel.exe.config.其他类型依次类推。
- 如果是VB程序的话,假如我们的程序名为test.exe,那么配置文件名称为test.exe.config.
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WCFServiceMoniker { public class Service1 : IService1 { public string SayHello(string yourwords) { return string.Format("Hello World! You entered: {0}{1}", yourwords,new string('A',65536) ); } } }
注:相比较以前的服务端,只是多增加了65536个A作为返回值。
同样,服务端的配置文件也要做相应的变更:关于服务定义一节更改成如下:
<system.serviceModel> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_ZXG" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="52428800" maxReceivedMessageSize="6553600" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="819200" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Message"> <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" /> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true" /> </security> </binding> </wsHttpBinding> </bindings> <services> <service name="WCFServiceMoniker.Service1" behaviorConfiguration="WCFServiceMoniker.Service1Behavior"> <!-- Service Endpoints --> <endpoint address="" binding="wsHttpBinding" bindingConfiguration ="WSHttpBinding_ZXG"
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>
注:比较以前的,主要是更改了maxReceivedMessageSize,maxBufferPoolSize, maxStringContentLength的值。
好了服务端更改好之后,下面我们需要对客户端进行部分更改。
- 首先我们要准备一个客户端的配置文件,该文件和我们一般的.net配置文件差不多,如下所示:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="5242880" maxReceivedMessageSize="655360" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="819200" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Message"> <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" /> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true" /> </security> </binding> </wsHttpBinding> </bindings> <client> <endpoint address="http://zxg/WCFServiceMoniker/Service1.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1" contract="IService1" name="WSHttpBinding_IService1"> <identity> <dns value="localhost" /> </identity> </endpoint> </client> </system.serviceModel> </configuration>
- 将配置文件命名为wscript.exe.config,并将其放到系统目录的system32目录中
- 我们可以使用以前的Client.dll文件,不需要做任何改动,不过,我们需要对CallWCFService_TypedContract.vbs文件微小修改,在标记字符串中增加 bindingConfiguration=WSHttpBinding_IService1,告诉系统使用这个配置文件中定义的绑定,修改之后的文件如下:
'--------------------------------------------------------------- ' 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, bindingConfiguration=WSHttpBinding_IService1" 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") msgbox serviceMoniker.SayHello("I am LazyBee,"+chr(13)) 'msgbox serviceMoniker.SayHello("Ok") Set serviceMoniker= nothing
至此,所有更改都已完成,可以直接运行测试,一切ok!好了我可以回家了……
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞