无论是Web应用程序还是Win应用程序,我们都会经常用到配置文件。WCF作为分布式开发的基础框架,在定义服务以及定义消费服务的客户端时,都使用了配置文件的方法。配置文件的重要性和实用性是大家所熟知的,它可以给我们WCF开发的灵活性上带来很大的提高。下面说说我学习使用配置文件的所得。
WCF的配置使用.NET Framework的System.Configuration配置系统。在Visual Studio中配置一个WCF服务时,如果是自托管宿主或Windows Services宿主
,则配置文件为App.confing,如果是IIS宿主,则配置文件为Web.config。
先来看看简单的system.ServiceModel结构
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><system.ServiceModel>
<services>
<service>
<endpoint/>
</service>
</services>
<bindings>
<!—定义一个或多个系统提供的binding元素,例如<basicHttpBinding>-->
<!—也可以是自定义的binding元素,如<customBinding>.-->
<binding>
<!—例如<BasicHttpBinding>元素.-->
</binding>
</bindings>
<behaviors>
<!—一个或多个系统提供的behavior元素.-->
<behavior>
<!—例如<throttling>元素.-->
</behavior>
</behaviors>
</system.ServiceModel>
(1)<services>
服务是在配置文件的 services 节中定义的。每个服务都有自己的 service 配置节。在Service中定义特定服务的address,binding,contract(也就是传说中重要的ABC)。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><services>
<servicename="WCFStudent.WCFStudentText"behaviorConfiguration="ServiceBehavior">
<endpointaddress=""binding="wsHttpBinding"contract="WCFStudent.IStuServiceContract"></endpoint>
<endpointaddress="mex"binding="mexHttpBinding"contract="IMetadataExchange"/>
</service>
</services>
<!--一个Service中可以有多个endpoint,这样就可以同时定制多个服务。如果address值为空,那么endpoint的地址就是默认的基地址(BaseAddress)。那么则需要在host中声明BaseAddress-->
(2)<bindings><!---->
<!-- Content type: Devdiv1. Transform: orcas2mtps.xslt. -->
此节包含标准绑定和自定义绑定的集合。每一项都是一个可由其唯一 name 进行标识的 binding 元素。服务通过用 name 与绑定进行链接来使用绑定。
个人感觉比较常用的是<basicHttpBinding>,<basicHttpBinding>等。由于属性比较多,在这里就不一一说明了。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><bindings>
<basicHttpBinding>
<bindingname="IStuServiceContract"closeTimeout="00:01:00"
openTimeout="00:01:00"receiveTimeout="00:10:00"sendTimeout="00:01:00"
bypassProxyOnLocal="false"transactionFlow="false"hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288"maxReceivedMessageSize="65536"
messageEncoding="Text"textEncoding="utf-8"useDefaultWebProxy="true"
allowCookies="false">
</basicHttpBinding>
</bindings>
<!--相应属性的设置大家可以参看MSDN.
http://msdn.microsoft.com/zh-cn/library/ms731361.aspx-->
(3)<behaviors>
此元素定义名为 endpointBehaviors 和 serviceBehaviors 的两个子集合。每个集合分别定义终结点和服务所使用的行为元素。每个行为元素由其唯一的 name 属性标识。如果需要指定服务在执行方面的相关特性时,就必须定义服务的behavior。在WCF中,定义behavior就可以设置服务的运行时属性,甚至于通过自定义behavior插入一些自定义类型。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><behaviors>
<serviceBehaviors>
<behaviorname="ServiceBehavior">
<!--为避免泄漏元数据信息,请在部署前将以下值设置为false并删除上面的元数据终结点-->
<serviceMetadatahttpGetEnabled="true"/>
<!--要接收故障异常详细信息以进行调试,请将以下值设置为true。在部署前设置为false以避免泄漏异常信息-->
<serviceDebugincludeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
(4)<client>
有的时候我们还需要定义<client>节点,主要定义客户端可以连接的终结点的列表。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><client>
<endpointaddress="http://localhost:39113/WCFServiceText/WCFStudentText.svc"
binding="wsHttpBinding"bindingConfiguration="WSHttpBinding_IStuServiceContract"
contract="ServiceReference1.IStuServiceContract"name="WSHttpBinding_IStuServiceContract">
<identity>
<dnsvalue="localhost"/>
</identity>
</endpoint>
</client>
<!--如果宿主是IIS,WIN应用程序调用WCF时,会自动在App.config中生成<client>节的设置-->
三种宿主形式的配置文件的声明基本上一样。如果使用svcutil.exe生成客户程序,会在svcutil.exe的根目录中生成一个配置文件,值得大家一看:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><?xmlversion="1.0"encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<bindingname="WSHttpBinding_IStuServiceContract"closeTimeout="00:01:00"
openTimeout="00:01:00"receiveTimeout="00:10:00"sendTimeout="00:01:00"
bypassProxyOnLocal="false"transactionFlow="false"hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288"maxReceivedMessageSize="65536"
messageEncoding="Text"textEncoding="utf-8"useDefaultWebProxy="true"
allowCookies="false">
<readerQuotasmaxDepth="32"maxStringContentLength="8192"maxArrayLength="16384"
maxBytesPerRead="4096"maxNameTableCharCount="16384"/>
<reliableSessionordered="true"inactivityTimeout="00:10:00"
enabled="false"/>
<securitymode="Message">
<transportclientCredentialType="Windows"proxyCredentialType="None"
realm=""/>
<messageclientCredentialType="Windows"negotiateServiceCredential="true"
algorithmSuite="Default"establishSecurityContext="true"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpointaddress="http://localhost:39113/WCFServiceText/WCFStudentText.svc"
binding="wsHttpBinding"bindingConfiguration="WSHttpBinding_IStuServiceContract"
contract="IStuServiceContract"name="WSHttpBinding_IStuServiceContract">
<identity>
<dnsvalue="localhost"/>
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
本文介绍了WCF服务配置的核心要素,包括服务定义、绑定配置及行为设置等内容,并详细解析了配置文件的各部分功能。
939

被折叠的 条评论
为什么被折叠?



