上篇文章中对WCF的配置放到App.config中,这样可以使程序更灵活、更具有扩展性。
下面说下配置文件中各个节点的含义。
服务端:
WCF配置文件节点放在<system.serviceModel></system.serviceModel>之间。是<configuration>的子节点
服务端的信息放到<services></services>节点之间。<services>是<system.serviceModel>的子节点。
<services>可以配置多个服务,每个服务用<service></service>来配置。<service>节点属性"name"表示服务中指定的实现类,要用命名空间+类名的方式赋值。
“behaviorConfiguration”属性表示服务的行为,这个值要跟<serviceBehaviors>中子节点的值一致(后面会有说明)。
1 <service name="WCFService.Calculate" behaviorConfiguration="metadataBehavior" >
地址是WCF中三要素之一,所以要配置地址。配置地址的节点是<baseAddresses>,做为<host>的子节点。这里的地址是一个基地址。
1 <host> 2 <baseAddresses> 3 <add baseAddress="http://127.0.0.1:9999/Calculate"/> 4 </baseAddresses> 5 </host>
服务部分配置完成,继续配置终结点。终结点用<endpoint>表示。<endpoint>也是<service>的子节点。
<endpoint>的属性“address”表示相对地址。相对地址和基地址组在一起,形成基地址/相对地址的形式,成为终结点的完整地址。相对地址也可以指定为""(空字符串),这样相对地址就跟基地址是一个地址。相对地址还可以指定为绝对地址,这样就可以覆盖基地址,使基地址不起作用。“binding”属性表示绑定的服务协议。“contract”表示定义的服务契约,使用命名空间+服务契约名称的方式赋值。
如果需要相对地址赋值的话,使用命名空间+实现契约接口的类名形式赋值。不推荐绝对地址,因为灵活性不强。
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>这句话表示元数据交换终结点的配置。因为WCF交换数据是通过元数据进行交换。
服务属性完整配置如下:
<services> <service name="WCFService.Calculate" behaviorConfiguration="metadataBehavior" > <host> <baseAddresses> <add baseAddress="http://127.0.0.1:9999/Calculate"/> </baseAddresses> </host> <endpoint address="" binding="wsHttpBinding" contract="WCFService.ICalculate"></endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services>
配置完服务属性,就该配置服务的行为了。行为可以被不同的服务引用,所以,是单独的。
表示服务行为的节点是<behaviors>,跟<services>是兄弟节点。因为带s,所以,也是可以添加多个行为的。
<serviceBehaviors>表示这个是服务行为,是服务行为的名字。如果不指定name属性,所有服务都使用这个行为。指定name属性,则只有指定的服务使用这个行为。
<serviceMetadata>表示交换元数据。httpGetEnabled="true"表示可以使用get请求方式。
行为配置代码:
1 <behaviors> 2 <serviceBehaviors> 3 <behavior name="metadataBehavior"> 4 <serviceMetadata httpGetEnabled="true" /> 5 </behavior> 6 </serviceBehaviors> 7 </behaviors>
这样一个完整的服务端配置就完成了。
服务端完整的配置如下:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <system.serviceModel> <services> <service name="WCFService.Calculate" behaviorConfiguration="metadataBehavior" > <host> <baseAddresses> <add baseAddress="http://127.0.0.1:9999/Calculate"/> </baseAddresses> </host> <endpoint address="" binding="wsHttpBinding" contract="WCFService.ICalculate"></endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="metadataBehavior"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
客户端配置
客户端配置比服务端要简单,在引用节点上,右键单击,选择“添加服务引用”,VS会自动生成配置文件。
客户端配置文件跟服务端配置文件差不多。服务端的配置理解后,看客户端的配置会感觉很容易理解。
客户端的配置也要放入<system.serviceModel></system.serviceModel>之间。
首先是协议的绑定:
<bindings> </bindings> 节点表示的是客户端和服务端通信使用的协议绑定。这个协议要跟服务端和客户端一致。
<wsHttpBinding></wsHttpBinding>表示这是一个wsHttpBinding类型的通信协议。
<binding>节点的name属性表示这个协议的名字,用来标识客户端endpoint用的是哪一个协议。
协议绑定配置:
<bindings> <wsHttpBinding> <binding name="WSHttpBinding_ICalculate" /> </wsHttpBinding> </bindings>
绑定完毕协议该进行客户端endpoint的配置了。
<client></client>节点表示这是一个客户端节点。
<endpoint>节点中address属性使用服务端公布出来的地址。binding属性表示终结点使用的协议类型。bindingConfiguration属性跟<bindings>中<binding>节点name属性相同,表明使用的是哪一个节点。contract表示使用的契约,使用命名空间+契约名称的形式赋值。name属性,表示这个endpoint节点的名称是什么,在使用ChannelFactory创建通道时候,使用这个属性。
<identity>表示这个配置文件的使用角色。
<client> <endpoint address="http://127.0.0.1:9999/Calculate" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICalculate" contract="WCFReference.ICalculate" name="WSHttpBinding_ICalculate"> <identity> <userPrincipalName value="SUN-PCAdministrator" /> </identity> </endpoint> </client>
系统自动生成的完整客户端配置如下:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_ICalculate" /> </wsHttpBinding> </bindings> <client> <endpoint address="http://127.0.0.1:9999/Calculate" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICalculate" contract="WCFReference.ICalculate" name="WSHttpBinding_ICalculate"> <identity> <userPrincipalName value="SUN-PCAdministrator" /> </identity> </endpoint> </client> </system.serviceModel> </configuration>