今天想验证一下System.ServiceModel.FaultException<T>,于是想通过一个Web页面调用WCF服务来弄一下。
可是了不幸的是Web层调用WCF的Client对象时报:“客户端配置部分中,找不到引用协定 的默认终结点元素……”,后来找了一下资料也算是解决了。
1. 服务端的配置(可以手写,也可用“WCF服务配置编辑器”)
1 <system.serviceModel> 2 3 <!--<services configSource="MyConfig\Debug\WCF.services.config"></services>--> 4 <!--<bindings configSource="MyConfig\Debug\WCF.bindings.config"></bindings>--> 5 <!--<client configSource="MyConfig\Debug\WCF.client.config"></client>--> 6 <!--<behaviors configSource="MyConfig\Debug\WCF.behaviors.config"></behaviors>--> 7 8 <bindings> 9 <basicHttpBinding> 10 <binding name="JsonHelperBinding" /> 11 </basicHttpBinding> 12 </bindings> 13 14 <client> 15 <remove contract="IMetadataExchange" name="sb" /> 16 </client> 17 18 <behaviors> 19 <serviceBehaviors> 20 <behavior name="JsonHelperServiceBehavior"> 21 <serviceMetadata httpGetEnabled="true" /> 22 </behavior> 23 </serviceBehaviors> 24 </behaviors> 25 26 <services> 27 <service behaviorConfiguration="JsonHelperServiceBehavior" name="ServiceImpl.JsonHelper"> 28 <endpoint address="" 29 binding="basicHttpBinding" 30 bindingConfiguration="JsonHelperBinding" 31 contract="IService.IJsonHelper" /> 32 33 <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 34 </service> 35 </services> 36 37 </system.serviceModel>
2. 客户端添加“服务引用”后自动生成的配置(app.config)
1 <system.serviceModel> 2 <bindings> 3 <basicHttpBinding> 4 <binding name="BasicHttpBinding_IJsonHelper" closeTimeout="00:01:00" 5 openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 6 allowCookies="false" 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://localhost:2566/SVCJsonHelper.svc" binding="basicHttpBinding" 22 bindingConfiguration="BasicHttpBinding_IJsonHelper" contract="JsonHelperService.IJsonHelper" 23 name="BasicHttpBinding_IJsonHelper" /> 24 </client> 25 </system.serviceModel>
3. 问题点:其实现在整个WCF服务已就绪,就是在Web层调用WCF客户端的时候报错了
1 ServicesClient.JsonHelperService.JsonHelperClient client = new ServicesClient.JsonHelperService.JsonHelperClient();
4. 我是这样解决的:
1. 在我们的Web层不是也有一个"Web.confing"文件嘛,在里面新增一个标签:“<system.serviceModel>...{0}...</system.serviceModel>” (当然,请在Web层引用“System.ServiceModel”).
2. 在客户端自动生成的那个配置文件(app.config)中,把<system.serviceModel></system.serviceModel>下的所有内容复制(Copy)到Web层,即上一步的{0}处