WCF服务在类库中的引用

在类库中引用了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电子政务平台 runk4.源代码框架后台服务服务内容ServiceContentInstantMessengerWiseUCService ReferencesWiseUCReference.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电子政务平台 runk4.源代码框架后台服务服务内容ServiceContentInstantMessengerWiseUCContentInstantMessengerWiseUC.cs:行号 242
在 FrameWorkService.Imp.ContentInstantMessengerWiseUC.SendMessage(String sender_userID, String acceptor_userID, String message) 位置 D:2.hWorking电子政务平台 runk4.源代码框架后台服务服务内容ServiceContentInstantMessengerWiseUCContentInstantMessengerWiseUC.cs:行号 97

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

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

 1 <system.serviceModel>
 2         <bindings>
 3             <basicHttpBinding>
 4                 <binding name="functionsBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
 5                     receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
 6                     bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
 7                     maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
 8                     messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
 9                     useDefaultWebProxy="true">
10                     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
11                         maxBytesPerRead="4096" maxNameTableCharCount="16384" />
12                     <security mode="None">
13                         <transport clientCredentialType="None" proxyCredentialType="None"
14                             realm="" />
15                         <message clientCredentialType="UserName" algorithmSuite="Default" />
16                     </security>
17                 </binding>
18             </basicHttpBinding>
19         </bindings>
20         <client>
21             <endpoint address="http://192.168.3.188:14132/Interface/www/soap/stdserver.php?wsdl"
22                 binding="basicHttpBinding" bindingConfiguration="functionsBinding"
23                 contract="WiseUC.functionsPortType" name="functionsPort" />
24         </client>
25     </system.serviceModel>
View Code

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

于是,我想到自已能不能用代码完成这一堆配置信息的设置而不再依赖于.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/

本博客所有内容均对所有人无条件共享,欢迎学习或转载,同时也希望您也加入我们一起推动知识界的共享。 www.mccn.pub
原文地址:https://www.cnblogs.com/liangjiang/p/4242065.html