WCF服务在类库中的引用

时间:2023-03-08 16:18:27

在类库中引用了WCF服务,悲剧降临了,追踪日志看到下边一串:

------------------------------------------------------------------------------

出错时间: 2015/1/22 14:54:40
出错信息: 在 ServiceModel 客户端配置部分中,找不到引用协定“WiseUC.functionsPortType”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素中找不到与此协定匹配的终结点元素。
详细信息: 在 System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName)
在 System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName, Configuration configuration)
在 System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName)
在 System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address)
在 System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress)
在 System.ServiceModel.EndpointTrait`1.CreateSimplexFactory()
在 System.ServiceModel.ClientBase`1.CreateChannelFactoryRef(EndpointTrait`1 endpointTrait)
在 System.ServiceModel.ClientBase`1.InitializeChannelFactoryRef()
在 System.ServiceModel.ClientBase`1..ctor()
在 ServiceContentInstantMessengerWiseUC.WiseUC.functionsPortTypeClient..ctor() 位置 D:\2.hWorking\电子政务平台\trunk\04.源代码\框架后台服务\服务内容\ServiceContentInstantMessengerWiseUC\Service References\WiseUC\Reference.cs:行号 243
在 FrameWorkService.Imp.ContentInstantMessengerWiseUC.bSpellInfoParameter(String cSenderName, String cReceiverNames, String cInfoTitle, String cInfoContent, String cLinkUrl, String cSendStyle, String cKeyCode, String cServiceUrl) 位置 D:\2.hWorking\电子政务平台\trunk\04.源代码\框架后台服务\服务内容\ServiceContentInstantMessengerWiseUC\ContentInstantMessengerWiseUC.cs:行号 242
在 FrameWorkService.Imp.ContentInstantMessengerWiseUC.SendMessage(String sender_userID, String acceptor_userID, String message) 位置 D:\2.hWorking\电子政务平台\trunk\04.源代码\框架后台服务\服务内容\ServiceContentInstantMessengerWiseUC\ContentInstantMessengerWiseUC.cs:行号 97

-------------------------------------------------------------------------------------------------------

说得很明白,是因为“未找到应用程序的配置文件,或者是因为客户端元素中找不到与此协定匹配的终结点元素”,可这句话几乎等于一句废话,因为我曾经信了这句话,将类库中的.config文件中的复制到了主程序中(就是那个有Main函数的.exe程序)的.config文件中,当时是管用了,但后来我又在另一个类库中引用了同样的WCF服务,但是悲剧再次降临。

 <system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="functionsBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://192.168.3.188:14132/Interface/www/soap/stdserver.php?wsdl"
binding="basicHttpBinding" bindingConfiguration="functionsBinding"
contract="WiseUC.functionsPortType" name="functionsPort" />
</client>
</system.serviceModel>

-------------------------------------------------------------------------------------------------------

于是,我想到自已能不能用代码完成这一堆配置信息的设置而不再依赖于.config文件中的这一堆,然后我万分辛苦地百度-试验-百度-试验,在绝望的终点终于发现了一篇博客,于是,我成功了,虽然这个成功看起来似乎并不完美,但是至少能解决眼前的大问题。(但这个做法目前无法控制传输的字符串长度与等待时间等参数,如果以后见到处理办法我会补上来,也希望诸位在其他地方或您就会的话,留个言或留个链接,不胜感谢!)

下边是我在项目中使用的源码:

string cServiceUrl="http://192.168.3.188:14132/Interface/www/soap/stdserver.php?wsdl";
ServiceContentInstantMessengerWiseUC.WiseUC.functionsPortTypeClient kClient = new ServiceContentInstantMessengerWiseUC.WiseUC.functionsPortTypeClient(new System.ServiceModel.BasicHttpBinding(), new System.ServiceModel.EndpointAddress(cServiceUrl));

ServiceContentInstantMessengerWiseUC.WiseUC.returnArr kReturn = kClient.sendmsgs(msg);
kClient.Close(); return kReturn.info;

重点在于实例化WCF客户端对象时的两个参数,加上这两个参数以后,那个类库中的.config文件就不再需要了,也不必在把.config中的那一堆配置复制到主程序中了。

-------------------------------------------------------------------------------------------------------

参考博客:http://blog.163.com/liuyang1285@126/blog/static/1289130862011626104116229/