您的位置:首页 > 其它

WCF中配置文件解析

2016-10-26 09:19 197 查看
在WCF
Service Configuration Editor的使用中,我们通过配置工具自动生成了WCF服务端的config文件。现在我们来看下这个配置文件各个标签的意义(解释在下面xml文件中的注释里)。

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <system.ServiceModel>

    <!-- 这个程序集我们在项目中有引用的 -->

    <!-- services 元素包含应用中驻留的所有service的 配置要求 -->

    <services>

      <!-- 每个服务的配置属性说明:

                name - 指 定这个service配置是针对的那个服务,为 一个实现了某些Contract的服务类的完全

                       限定名(名称空间.类型名),ServiceHost载入一个服务后,会 到配置文件中的 下找有

                       没有name属性跟服务匹配的 的配置

                behaviorConfiguration - 指定在下的一个的name,这个特定给这个service制定了一些行为,

                       比 如服务是否允许身份模拟-->

      <service name="名称空间.类型名" behaviorConfiguration="behavior名">

        <host>

          <baseAddresses>

            <!-- 在此可以定义每种传输协议的baseAddress, 用于跟使用同样传输协议Endpoint定义的相对地

                    址组成完整的地址,但是每种传输协议只能定义一个baseAddress。HTTP的baseAddress同时是service

                    对外发布服务说明页面的URL-->

            <add baseAddress="http://address" />

          </baseAddresses>

          <timeouts></timeouts>

        </host>

        <!-- 每个服务可以有多个Endpoint, 下面 元素对每个Endpoint分别进行 配置

                 属性说明:

                 address - 指 定这个Endpoint对外的URI,这个URI可以是个绝对地址,也可以是个相对于baseAddress的

                           相对地址。如果此属性为空,则这 个Endpoint的地址就是baseAddress

                 binding - 指定这个Endpoint使用的binding,这个banding可以是系统预定义的9个binding之一,

                           比如是basicHttpBinding,也可以是自定义的customBinding。binding决定了通讯 的类型、

                           安全、如何编码、是否基于session、是否基于事务等等

                 contract - 指定这个Endpoint对应的Contract的全限定名(名称空间.类型名),这个Contract应 该被

                            service元素的name指定的那个service实现

                 bindingConfiguration - 指 定一个binding的配置名称,跟下面同 类的name匹配

                    behaviorConfiguration - 指 定这个endpoint的behavior, 指向 下的同样配置名称的

                 name - Endpoint的名称,可选属性,每个Contract都可以有多个Endpoint,但是每个Contract对应的

                        多个Endpoint名必须是唯一的-->

        <endpoint address="URI" binding="basicHttpBinding" contract="Contract全限定名" bindingConfiguration="binding名" behaviorConfiguration="String" name="">

          <!-- 用户定义的xml元素集合, 一般用作SOAP的header内容-->

                   <!-- 这个可以不填,但当多个endpoint都用的是一个地址时,header就显得非常重要了-->

          <headers>

            <!-- 任何xml内容 -->

          </headers>

        </endpoint>

      </service>

    </services>

    <bindings>

      <!-- 指定一个或多个系统预定义的binding, 比如,当然也可以指定自定义的customBinding,

             然后在某个指定的binding下建立一个或多个配置,以便被Endpoint来 使用这些配置 -->

      <!-- 这里举两个Binding的方式,一个是http的,一个是tcp的,BindingElement的顺序非常重要。

                   BindingElement集合通常会创建一个用于通信的堆栈,起顺序语BindingElement集 合中元素顺序一致。

                    因此BindingElement集合中的binding element顺序直接影响了通信堆栈处理消息的顺序。不过这些WCF都为你排好序了,不必担心。-->

       <basicHttpBinding>

        <!-- 某一类的binding的下面可能有多个配置,binding元素的name属性标识某个binding-->

        <binding name="binding名">

        </binding>

      </basicHttpBinding>

      <netTcpBinding> </netTcpBinding> 

    </bindings>

    <!-- 定义service和Endpiont行为-->

    <behaviors>

      <!-- 定义service的行为-->

      <serviceBehaviors>

        <!-- 一个或多个系统提供的或定制的behavior元 素

                 属性说明:

                 name - 一个behavior唯一标识,元素下的behaviorConfiguration属 性指向这个name-->

        <behavior name="此Behavior名称">

          <!-- 指定service元数据发 布和相关信息

                     属性说明:

                     httpGetEnabled - bool类型的值,表示是否允许通过HTTP的get方 法获取sevice的WSDL元数据

                     httpGetUrl - 如 果httpGetEnabled为true, 这个属性指示使用哪个URL地址发布服务的WSDL,

                                  如果这个属性没有设置,则使用服务的HTTP类型的baseAddress后面加上?WSDL-->

          <serviceMetadata httpGetEnabled="true" httpGetUrl="http://URI:port/address" />

          <!--指定验证服务端的凭据-->

          <serviceCredentials>

            <!--指定服务端的证书

                         属性说明:

                         storeName - 证书的存储区,可能值为:AddressBook,AuthRoot,CertificateAuthority

                                     Disallowed,My,Root,TrustedPeople,TrustedPublisher

                         storeLocation - 证书存储位置,可能值为:CurrentUser,LocalMachine

                             x509FindType - 查找证书的方式,可能的值:FindBySubjectName,FindByThumbPrint,FindByIssuerName......

                             findValue - 对应查找方式的要查找证书的值                  -->

            <serviceCertificate storeName="存储区" storeLocation="存储位置" x509FindType="FindBySubjectName" findValue="server1" />

          </serviceCredentials>

        </behavior>

      </serviceBehaviors>

      <!-- 定义Endpiont的行为-->

      <endpointBehaviors>

        <!-- 一个或多个系统提供的或定制的behavior元 素

                 属性说明:

                 name - 一个behavior唯一标识,元素下的behaviorConfiguration属 性指向这个name-->

        <behavior name="此Behavior名称">

          <!--指定客户端的凭据-->

          <clientCredentials>

            <!--指定客户端的证书

                         属性说明:

                         storeName - 证 书的存储区,可能值为:AddressBook,AuthRoot,CertificateAuthority

                                     Disallowed,My,Root,TrustedPeople,TrustedPublisher

                         storeLocation - 证书存储位置,可能值为:CurrentUser,LocalMachine

                             x509FindType - 查找证书的方式,可能的值:FindBySubjectName,FindByThumbPrint,FindByIssuerName......

                             findValue - 对应查找方式的要查找证书的值                  -->

            <clientCertificate storeName="存储区" storeLocation="存储位置" x509FindType="FindBySubjectName" findValue="Client1" />

            <serviceCertificate>

              <authentication certificateValidationMode="None" />

            </serviceCertificate>

          </clientCredentials>

        </behavior>

      </endpointBehaviors>

    </behaviors>

    <!-- 包含客户端跟服务端连接使用到的Endpoint的 配置 -->

    <client>

      <!-- 每个客户端Endpoint设置 服务端的配置没有这个标签

             属性说明:

             address - 对 应到服务端这个Endpoint的address

             binding - 指定这个Endpoint使用的binding,这个banding可以是系统预定义的9个binding之一,

                       比如是basicHttpBinding

             contract - 指 定这个Endpoint对应的Contract的 全限定名(名称空间.类型名)

             name - Endpoint的配置名,客户端代理类的构造方法中的endpointConfigurationName对 应到这个name

             bindingConfiguration - 指 定客户端binding的具体设置,指向元 素下同类型binding的name

               behaviorConfiguration - 指定这个endpoint的behavior,指向 下的同样配置名称的 -->

      <endpoint address="URI" binding="basicHttpBinding" bindingConfiguration="binding名" behaviorConfiguration="String" contract="Contract全限定名" name="endpoint配置名" >

        <!-- 用于客户端验证服务端身份,可选以下一种方式验证服务端-->

        <identity>

          <userPrincipalName></userPrincipalName>

          <servicePrincipalName></servicePrincipalName>

          <!--如果客户端验证是windows,这里指定DNS名;如果是Certificate, 这里指定证书subject name-->

          <dns></dns>

          <rsa></rsa>

          <!--指定服务端证书的公钥

                            属性说明:

                            encodedValue - 服务端证书的公钥的base64编码,用于加密用户名和 密码-->

          <certificate encodedValue=""></certificate>

          <!-- 用户指定在客户端证书存储区内的服务端证书

                         属性说明:

                         storeName - 证 书的存储区,可能值为:AddressBook,AuthRoot,CertificateAuthority

                                     Disallowed,My,Root,TrustedPeople,TrustedPublisher

                         storeLocation - 证书存储位置,可能值为:CurrentUser,LocalMachine

                             x509FindType - 查找证书的方式,可能的值:FindBySubjectName,FindByThumbPrint,FindByIssuerName......

                             findValue - 对应查找方式的要查找证书的值                  -->

          <certificateReference storeName="存储区" storeLocation="存储位置" x509FindType="FindBySubjectName" findValue="Client1" />

        </identity>

      </endpoint>

    </client>

  </system.ServiceModel>

</configuration>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息