WCF配置
一、服务端配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<system.ServiceModel>
<services>
<service>
<host/>
<endpoint/>
</service>
</services>
<behaviors>
<behavior>
</behavior>
</behaviors>
<bindings>
<binding>
</binding>
</bindings>
</system.ServiceModel>
</configuration>
1.<System.ServiceModel>配置节包含了所有的WCF配置信息,其位于根配置节Configuration之下。
ServiceModel节点有3个部分
- <Services>节点
- <Behaviors>节点
- <Bindinds>节点
(一)<Services>节点
<Services>节点下,可以有很多个<service>节点。
<service>节点有两个属性:name和behaviorConfiguration.
1.Name属性是必须配置的,它的值应该是实现服务的类的名称,并包含它的命名空间在内(即命名空间+类名)。
2.BehaviorConfiguration属性用来指定服务的行为名称,可以为空(这里的Behavior,其实是 Service Behavior,后面我会介绍endpointBehaviors)。BehaviorConfiguration属性的值应该在后面的<behviors>配置节下被定义,否则WCF服务端将无法启动。
3.在<service>标签下,还需要包含一些子节点来定义服务的信息。
<host>
<baseAddresses>
<add baseAddress=""/>
</baseAddresses>
</host>
<endpoint address="" binding="wsHttpBinding" contract="" bindingConfiguration="" />
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
3.1<host>节点,这个节点指定服务的宿主信息。<host>下可以包含以下子节点。
A.<baseAddresses>节点:指定服务的访问地址。使用<add baseAddress=""/>标签进行地址的添加,可以添加多个地址。根据WCF宿主程序的不同,地址会有不同的形式。
B.<timeouts>节点:用来指定服务启动或关闭的间隔。可以使用closeTimeout 和 openTimeout两个属性进行控制。
C.<endpoint>节点:WCF需要在endpoint节点中指定服务指定的服务契约类型、通信绑定(Binding)和Behavior的绑定。<endpoint>节点有如下的属性用来指定上述信息。
<address>:指定enpoint的地址。地址必须为合法的Url格式,可以使相对路径也可以是绝对路径。address属性值可以为空,但是这个属性必须存在。如果address值为空,那么地址就是之前定义的baseAddresses中的地址。
<bindingConfiguration>:可选属性。除非有自定义的绑定策略,否则不要设置这个值。
<bingding>:指定使用的绑定策略,此属性为必要属性。使用的绑定策略师<binding>节点下定义的。
<contract>:该属性也是必须属性。它指定服务契约的类型,推荐使用接口来定义契约,并在这里使用接口的全名称。(如果不是有接口定义,也可以使用命名空间+类名来定义)。
<behaviorConfiguration>:可选属性,指定要使用的behavior配置的名称。
一个应用程序可以配置多个<endpoint>.
WCF 配置系列(二)
<Behaviors>节点配置
<behaviors>节点中的配置信息定义了服务运行和执行的相关特性。Behavior可以分为endpointBehaviors和ServiceBehaviors,因此<behaviors>节点下支持两个子节点:<servcieBehaviors>和<endpointBehaviors>。在这两个字节点中可以包含一个或者多个behavior定义。
<serviceBehaviors>节点中包含了所有关于服务运行时行为
<endpointBehaviors>节点中则包含了跟endpoint有关的特性。
行为可以在程序中使用标记(attribute)来定义,也可以在配置文件中指定,但是应该尽量在配置文件中指定behavior特殊(这样灵活、部署方便)。
<behaviorname="">
<serviceMetadatahttpGetEnabled="true"httpsGetEnabled="true"/>
<serviceDebugincludeExceptionDetailInFaults="true"/>
</behavior>
这里定义了两个特性,
一个是元数据服务(允许服务公开元数据访问)。
另一个是指定返回错误的详细信息。
serviceBehavior中可以定义的所有特性控制如下所示:包括数据契约的序列化、持久化提供者、服务授权、服务验证、服务调式、元数据、安全审计、服务调整、服务超时和工作流等。
endpointBehaviors,支持的特性如下:
WCF 配置系列(三)
<Binding>节点配置
<bindings>节点包含了通信特性的定义,这些特性包含通信协议、传输机制和编码、解码器。
Bindings节点可以包含一个或多个<binding>子节点,每个<binding>子节点定义一部分endpoint之间的通信特性。不同的Binding配置节对应着不同的通信特性,有的Binding节点配置协议通道,有的配置传输通道,还有的配置编码器。虽然可以有多个Binding子节点,但是关于传输通道和编码通道的Binding节点都只能有一个,协议通道除外。
系统内置支持大约十多种Binding,要使用它们,除了要在endpoint节点处使用binding属性指定要使用哪种类型的Binding,还要在Bindings节点下定义一个同类型的Binding(可以不定义,这种情况下程序使用此Binding类型的默认特性)。示例如下。
<services>
<servicename="WcfService"behaviorConfiguration="serviceBehavior">
<endpointaddress=""binding="wsHttpBinding"bindingConfiguration="bindingName"
contract="WcfService">
</endpoint>
</service>
</services>
<bindings>
<wsHttpBinding>
<bindingname="bindingName">
</binding>
</wsHttpBinding>
</bindings>
也可以不定义bindings中的wsHttpBinding节点,直接在endpoint定义中指定Binding类型,代码如下。
<endpointaddress=""binding="wsHttpBinding"contract="WcfService">
</endpoint>
系统自带的binding有下面这些。
l<BasicHttpBinding>
l<WSHttpBinding>
l<WSDualHttpBinding>
l<WSFederationHttpBinding>
l<WebHttpBinding>
l<NetTcpBinding>
l<NetNamedPipeBinding>
l<NetMsmqBinding>
l<NetPeerTcpBinding>
l<MsmqIntegrationBinding>
WCF 配置系列(四) ---客户端配置
WCF最重要的是服务端的配置,客户端的配置大多根据服务端的配置不同而相应地改变。客户端的配置主要包含两个部分:
一个是导出的服务端Binding配置,
另一个是Client配置节点。
客户端配置示例如下:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<bindingname="WSHttpBinding_ClassName"closeTimeout="00:01:00"
openTimeout="00:01:00"receiveTimeout="00:10:00"sendTimeout="00:01:00"
bypassProxyOnLocal="false"transactionFlow="false"hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="6553600"maxReceivedMessageSize="6553600"messageEncoding="Text"
textEncoding="utf-8"useDefaultWebProxy="true"allowCookies="false">
<readerQuotasmaxDepth="32"maxStringContentLength="819200"maxArrayLength="1638400"
maxBytesPerRead="409600"maxNameTableCharCount="1638400"/>
<reliableSessionordered="true"inactivityTimeout="00:10:00"
enabled="false"/>
<securitymode="None">
<transportclientCredentialType="Windows"proxyCredentialType="None"
realm=""/>
<messageclientCredentialType="Windows"negotiateServiceCredential="true"
establishSecurityContext="true"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpointaddress="http://localhost/WcfService/ServiceName/"
binding="wsHttpBinding"bindingConfiguration="WSHttpBinding_ClassName"
contract="Services.IService"name="WSHttpBinding_ClassName">
<identity>
<dnsvalue="localhost"/>
</identity>
</endpoint>
</client>
</system.serviceModel>
其实,这两个配置节点在添加服务引用时会自动根据服务端的信息生成,大多数时候不需要客户端进行修改。需要注意的是,如果服务端配置了多个endpoint,那么客户端只能获得当前使用的endpoint的信息,需要手动添加其他endpoint信息。另外,导出的Bindings配置节中的信息比较多,VS自动把服务端配置信息的默认值也加入了配置文件