WCF services配置节问题解析

在WCF的快速发展,它的性能也随之增长,但是有很多人都对配置文件很头疼,现在就教教大家吧。在WCF services配置节中可以定义多个服务,每一个服务都被放到service配置节中,WCF的宿主程序可以通过配置文件找到这些定义的服务并发布这些服务。WCF services配置节包含name和behaviorConfiguration属性。其中,name配置了实现ServiceContract的类型名。类型名必须是完整地包含了命名空间和类型名。

#T#而behaviorConfiguration的配置值则与其后的behaviors配置节的内容有关。endpoint是service配置节的主体,其中,endpoint配置节包含了endpoint的三个组成部分:Address、Binding和Contract。由于具体的binding配置是在bindings配置节中完成,因而,在endpoint中配置了bindingConfiguration属性,指向具体的binding配置。如下所示:

 
 
  1. services  
  2. servicename="BruceZhang.MyService"behaviorConfiguration="MyBehavior"  
  3. endpointaddress="" 
  4. binding="netTcpBinding" 
  5. bindingConfiguration="DuplexBinding" 
  6. contract="BruceZhang.IHello"/  
  7. /service  
  8. /services 

我们也可以定义多个endpoint,例如:

 
 
  1. services  
  2. service  
  3. name="Microsoft.ServiceModel.Samples.CalculatorService" 
  4. behaviorConfiguration="CalculatorServiceBehavior" 
  5. endpointaddress="" 
  6. binding="wsHttpBinding" 
  7. contract="Microsoft.ServiceModel.Samples.ICalculator"/  
  8. endpointaddress="mex" 
  9. binding="mexHttpBinding" 
  10. contract="Microsoft.ServiceModel.Samples.IMetadataExchange"/  
  11. /service  
  12. /services 

如果address值为空,那么endpoint的地址就是默认的基地址(BaseAddress)。例如ICalculator服务的地址就是http://localhost/servicemodelsamples/service.svc,而IMetadataExchange服务的地址则为http://localhost/servicemodelsamples/service.svc/mex。这里所谓的基地址可以在WCF services配置节中通过配置host来定义:

 
 
  1. service  
  2. name="Microsoft.ServiceModel.Samples.CalculatorService" 
  3. behaviorConfiguration="CalculatorServiceBehavior" 
  4. host  
  5. baseAddresses  
  6. addbaseAddress=  
  7. "http://localhost/ServiceModelSamples/service.svc"/  
  8. /baseAddresses  
  9. /host  
  10. endpoint…/  
  11. /service  
THE END