在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配置。如下所示:
- services
- servicename="BruceZhang.MyService"behaviorConfiguration="MyBehavior"
- endpointaddress=""
- binding="netTcpBinding"
- bindingConfiguration="DuplexBinding"
- contract="BruceZhang.IHello"/
- /service
- /services
我们也可以定义多个endpoint,例如:
- services
- service
- name="Microsoft.ServiceModel.Samples.CalculatorService"
- behaviorConfiguration="CalculatorServiceBehavior"
- endpointaddress=""
- binding="wsHttpBinding"
- contract="Microsoft.ServiceModel.Samples.ICalculator"/
- endpointaddress="mex"
- binding="mexHttpBinding"
- contract="Microsoft.ServiceModel.Samples.IMetadataExchange"/
- /service
- /services
如果address值为空,那么endpoint的地址就是默认的基地址(BaseAddress)。例如ICalculator服务的地址就是http://localhost/servicemodelsamples/service.svc,而IMetadataExchange服务的地址则为http://localhost/servicemodelsamples/service.svc/mex。这里所谓的基地址可以在WCF services配置节中通过配置host来定义:
- service
- name="Microsoft.ServiceModel.Samples.CalculatorService"
- behaviorConfiguration="CalculatorServiceBehavior"
- host
- baseAddresses
- addbaseAddress=
- "http://localhost/ServiceModelSamples/service.svc"/
- /baseAddresses
- /host
- endpoint…/
- /service