原文地址:http://www.51gbk.com/a/websucai/WCF/2014/0801/501.html
使用了IIS做为宿主,客户端调用WCF服务的是Web应用程序。今天这个小节主要以介绍WCF中传输的配置为主,我们把上一节的内容稍做改动,以体现出"Endpoint"与"A、B、C"。
由于我在教程一里没有手写任何配置的代码,Client与Service端的Web.config都是自动生成的,当我们添加服务引用时,IDE自动将客户端的配置文件中Endpoint与引用的服务的Endpoint匹配了。如下代码所示:
客户端Web.config:
1 <?xml version="1.0"encoding="utf-8"?>
2
3 <!--
4 有关如何配置 ASP.NET 应用程序的详细消息,请访问
5 http://go.microsoft.com/fwlink/?LinkId=169433
6 -->
7
8 <configuration>
9 <system.web>
10 <compilation debug="true" targetFramework="4.0" />
11 </system.web>
12
13 <system.serviceModel>
14 <bindings>
15 <basicHttpBinding>
16 <binding name="BasicHttpBinding_IUser" closeTimeout="00:01:00"
17 openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
18 allowCookies="false" bypassProxyOnLocal="false"hostNameComparisonMode="StrongWildcard"
19 maxBufferSize="65536" maxBufferPoolSize="524288"maxReceivedMessageSize="65536"
20 messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
21 useDefaultWebProxy="true">
22 <readerQuotas maxDepth="32" maxStringContentLength="8192"maxArrayLength="16384"
23 maxBytesPerRead="4096" maxNameTableCharCount="16384" />
24 <security mode="None">
25 <transportclientCredentialType="None"proxyCredentialType="None"
26 realm="" />
27 <messageclientCredentialType="UserName"algorithmSuite="Default" />
28 </security>
29 </binding>
30 </basicHttpBinding>
31 </bindings>
32 <client>
33 <endpoint address="http://localhost/User.svc" binding="basicHttpBinding"
34 bindingConfiguration="BasicHttpBinding_IUser" contract="WCFService.IUser"
35 name="BasicHttpBinding_IUser"/>
36 </client>
37 </system.serviceModel>
38 </configuration>
服务端Web.config代码:
1 <?xml version="1.0"encoding="utf-8"?>
2 <configuration>
3
4 <system.web>
5 <compilation debug="true"targetFramework="4.0" />
6 </system.web>
7 <system.serviceModel>
8 <behaviors>
9 <serviceBehaviors>
10 <behavior>
11 <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为false 并删除上面的元数据终结点-->
12 <serviceMetadata httpGetEnabled="true"/>
13 <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为true。在部署前设置为false以避免泄漏异常信息 -->
14 <serviceDebugincludeExceptionDetailInFaults="false"/>
15 </behavior>
16 </serviceBehaviors>
17 </behaviors>
18 <serviceHostingEnvironmentmultipleSiteBindingsEnabled="true"/>
19 </system.serviceModel>
20 <system.webServer>
21 <modulesrunAllManagedModulesForAllRequests="true"/>
22 </system.webServer>
23
24 </configuration>
由上面的两个配置文件我们发现,客户端system.serviceMode节点有我们刚才讲的endpoint,而服务端为什么没有?这不是和我们刚才讲的有违背吗?好吧,我们承认IDE生成的Web.config中endpoint很隐晦。那么我们看下面手工修改后[把对看起来很复杂并且对当前的学习无用的配置节删掉]的配置文件的代码(我将服务端和客户端放在一块了):
1 <!--Service Web.config-->
2 <?xml version="1.0"encoding="utf-8"?>
3 <configuration>
4 <system.web>
5 <compilation debug="true"targetFramework="4.0" />
6 </system.web>
7 <system.serviceModel>
8 <services>
9 <service name="WCFService">
10 <endpoint address="http://localhost/User.svc" binding="basicHttpBinding"
11 contract="WCFService.IUser"/>
12 </service>
13 </services>
14 </system.serviceModel>
15 </configuration>
16
17 <!--Client Web.config-->
18 <?xml version="1.0" encoding="utf-8"?>
19 <configuration>
20 <system.web>
21 <compilation debug="true" targetFramework="4.0" />
22 </system.web>
23 <system.serviceModel>
24 <client>
25 <endpoint address="http://localhost/User.svc" binding="basicHttpBinding"
26 contract="WCFService.IUser"/>
27 </client>
28 </system.serviceModel>
29 </configuration>
修改配置文件后我们执行下,发现WCF服务依然执行成功!!这次的配置文件够明显了吧,不论是在服务端还是在客户端,endpoint中的A、B、C都是完全一样的。也就是终结点完全匹配!
那么第一次的配置文件为什么能执行呢?答案是我们把WCF寄宿在IIS上,而IIS默认监听的就是Http协议[B确定了]并且地址也是相对于IIS上的文件地址[A确定了],合同更不用说了,找到User.svc什么都有了[C确定了],所以在服务端就没有必要显示的写出system.serviceModel,不信你试试,把服务端的配置文件中system.serviceModel节删除,程序一样可以运行!服务器端的endpoint确定了,客户端的endpoint自然要和服务端去对应,所以IDE在生成客户端的配置文件里endpoint写的很详细的,而服务端却没有endpoint。