上篇文章主要讨论了WCF的基本内容,其中包括WCF的术语、创建方法及WCF在开发过程中使用的意义,它不仅能够提供程序之间的通信,而且还能提供程序和数据间的通信,WCF提供了多样化的程序之间的通信,不仅支持App的通信而且还支持web与应用程序之间的通信,可谓是功能强大。虽然上文讨论了WCF的基本使用方法,并做了很多Demo,但是都只是关于WCF的创建和调用的,今天来看看WCF的配置方法。
WCF 配置导图
上图整理了服务配置过程中所用到的基本的元素,大致的步骤主要是首先要在调用服务的程序集中添加服务的一个引用,然后添加一个service并指定服务的名称、终结点,如果添加了behavior(行为)的配置,那么也需要添加一个behaviorConfiguration的配置信息。在添加一个service时会在其中指定终结点的信息,终结点说的就是服务的具体信息访问方式,在终结点中添加服务address及binding信息和contract(服务契约)信息等。在endpoint中添加的binding只是指定了服务绑定的访问方式,例如:basicHttpBinding等,真正的binding配置是需要在另外的binding中添加的,添加好后需要配置在endpoint的bindingConfiguration。下面看一个服务在客户端部署配置的示例。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<!--The bindings configuration
We deployed the basichttpbinding.
I added a binding that name is binding1.
The binding1 used the security.
-->
<bindings>
<basicHttpBinding>
<binding name="binding1">
<security mode="Message">
<message clientCredentialType="Certificate"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<!--The behaviors configuration
I added a serviceBehavior that has nothing configuration in.
-->
<behaviors>
<serviceBehaviors>
<behavior name="service1_behavior">
</behavior>
</serviceBehaviors>
</behaviors>
<!--The services configuration
I added a service that behaviorConfiguration is service1_behavior.
This service has an endpoint that added the property and the binding.
-->
<services>
<service name&#