客户端配置Client Configuration

您可以使用 Windows Communication Foundation (WCF)客户端配置来指定客户端终结点的地址、绑定、行为和协定,客户端终结点的 "ABC" 属性用于连接到服务终结点。You can use the Windows Communication Foundation (WCF) client configuration to specify the address, binding, behavior, and contract, the "ABC" properties of the client endpoint, which clients use to connect to service endpoints. <client>元素有一个 <endpoint> 元素,其属性用于配置终结点 abc。The <client> element has an <endpoint> element whose attributes are used to configure the endpoint ABCs. 这些属性在配置终结点部分中进行了讨论。These attributes are discussed in the Configuring Endpoints section.

<endpoint>元素还包含一个 <metadata> 元素,该元素用于指定导入和导出元数据的设置、 <headers> 包含自定义地址标头集合的元素,以及一个 <identity> 元素,通过该元素可以通过与消息交换消息的其他终结点对该终结点进行身份验证。The <endpoint> element also contains a <metadata> element that is used to specify settings for importing and exporting metadata, a <headers> element that contains a collection of custom address headers, and an <identity> element that enables the authentication of an endpoint by other endpoints exchanging messages with it. <headers><identity> 元素是的一部分 EndpointAddress ,并在 "地址" 一文中进行讨论。The <headers> and <identity> elements are part of the EndpointAddress and are discussed in the Addresses article. 配置元数据部分中提供了指向说明元数据扩展使用的主题的链接。Links to topics that explain the use of metadata extensions are provided in the Configuring Metadata section.

配置终结点Configuring Endpoints

客户端配置旨在允许客户端指定一个或多个终结点,每个终结点都有自己的名称、地址和协定,每个终结点都引用 <bindings> <behaviors> 客户端配置中要用于配置该终结点的和元素。The client configuration is designed to allow the client to specify one or more endpoints, each with its own name, address, and contract, with each referencing the <bindings> and <behaviors> elements in the client configuration to be used to configure that endpoint. 应将客户端配置文件命名为 "App.config",因为这是 WCF 运行时需要的名称。The client configuration file should be named "App.config" because this is the name that the WCF runtime expects. 下面的示例演示一个客户端配置文件。The following example shows a client configuration file.

<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
  <system.serviceModel>  
        <client>  
          <endpoint  
            name="endpoint1"  
            address="http://localhost/ServiceModelSamples/service.svc"  
            binding="wsHttpBinding"  
            bindingConfiguration="WSHttpBinding_IHello"  
            behaviorConfiguration="IHello_Behavior"  
            contract="IHello" >  
  
            <metadata>  
              <wsdlImporters>  
                <extension  
                  type="Microsoft.ServiceModel.Samples.WsdlDocumentationImporter, WsdlDocumentation"/>  
              </wsdlImporters>  
            </metadata>  
  
            <identity>  
              <servicePrincipalName value="host/localhost" />  
            </identity>  
          </endpoint>  
            <!-- Add another endpoint by adding another <endpoint> element. -->
          <endpoint  
            name="endpoint2">  
           //Configure another endpoint here.  
          </endpoint>  
        </client>  
  
//The bindings section references by the bindingConfiguration endpoint attribute.  
    <bindings>  
      <wsHttpBinding>  
        <binding name="WSHttpBinding_IHello"
                 bypassProxyOnLocal="false"
                 hostNameComparisonMode="StrongWildcard">  
          <readerQuotas maxDepth="32"/>  
          <reliableSession ordered="true"
                           enabled="false" />  
          <security mode="Message">  
           //Security settings go here.  
          </security>  
        </binding>  
        <binding name="Another Binding"  
          <!-- Configure this binding here. -->  
        </binding>  
          </wsHttpBinding>  
     </bindings>  
  
//The behavior section references by the behaviorConfiguration endpoint attribute.  
        <behaviors>  
            <endpointBehaviors>  
                <behavior name=" IHello_Behavior ">  
                    <clientVia />  
                </behavior>  
            </endpointBehaviors>  
        </behaviors>  
  
    </system.serviceModel>  
</configuration>  

可选的 name 属性唯一地标识了给定协定的终结点。The optional name attribute uniquely identifies an endpoint for a given contract. 它由 ChannelFactory<TChannel>ClientBase<TChannel> 用于指定客户端配置中的哪个终结点是目标终结点,必须在创建到服务的通道时加载。It is used by the ChannelFactory<TChannel> or by the ClientBase<TChannel> to specify which endpoint in the client configuration is being targeted and must be loaded when a channel is created to service. 可以使用通配符终结点配置名称 " * ",并向 ApplyConfiguration 方法指示它应在文件中加载任何终结点配置(前提是只有一个可用的终结点配置),否则会引发异常。A wildcard endpoint configuration name "*" is available and indicates to the ApplyConfiguration method that it should load any endpoint configuration in the file, provided there is precisely one available, and otherwise throws an exception. 如果省略此属性,则将对应的终结点用作与指定协定类型相关联的默认终结点。If this attribute is omitted, the corresponding endpoint is used as the default endpoint associated with the specified contract type. name 属性的默认值是一个空字符串,它与任何其他名称一样进行匹配。The default value for the name attribute is an empty string which is matched like any other name.

每个终结点都必须具有一个与之关联的地址,用于查找和标识终结点。Every endpoint must have an address associated with it to locate and identify the endpoint. address 属性可用来指定提供终结点位置的 URL。The address attribute can be used to specify the URL that provides the location of the endpoint. 但是,通过创建统一资源标识符 (URI),也可以在代码中指定服务终结点的地址,使用 ServiceHost 方法之一可以将该地址添加到 AddServiceEndpointBut the address for a service endpoint can also be specified in code by creating a Uniform Resource Identifier (URI) and is added to the ServiceHost using one of the AddServiceEndpoint methods. 有关详细信息,请参阅地址For more information, see Addresses. 正如简介中所述, <headers><identity> 元素是的一部分,并且 EndpointAddress 还在 "地址" 主题中进行了讨论。As the introduction indicates, the <headers> and <identity> elements are part of the EndpointAddress and are also discussed in the Addresses topic.

binding 属性指示终结点在连接到服务时期望使用的绑定类型。The binding attribute indicates the type of binding the endpoint expects to use when connecting to a service. 该类型必须具有一个已注册的配置节,才能加以引用。The type must have a registered configuration section if it is to be referenced. 在上面的示例中,这是 <wsHttpBinding> 部分,它指示终结点使用 WSHttpBindingIn the previous example, this is the <wsHttpBinding> section, which indicates that the endpoint uses a WSHttpBinding. 实际上,终结点可以使用某个给定类型的多个绑定。But there may be more than one binding of a given type that the endpoint can use. 其中每个都在 <binding> (绑定)类型元素中具有自己的元素。Each of these has its own <binding> element within the (binding) type element. bindingconfiguration 属性用于区分相同类型的绑定。The bindingconfiguration attribute is used to distinguish between bindings of the same type. 其值与元素的属性相匹配 name <binding>Its value is matched with the name attribute of the <binding> element. 有关如何使用配置来配置客户端绑定的详细信息,请参阅如何:在配置中指定客户端绑定For more information about how to configure a client binding using configuration, see How to: Specify a Client Binding in Configuration.

behaviorConfiguration特性用于指定 <behavior> <endpointBehaviors> 终结点应使用哪一个。The behaviorConfiguration attribute is used to specify which <behavior> of the <endpointBehaviors> the endpoint should use. 其值与元素的属性相匹配 name <behavior>Its value is matched with the name attribute of the <behavior> element. 有关使用配置来指定客户端行为的示例,请参阅配置客户端行为For an example of using configuration to specify client behaviors, see Configuring Client Behaviors.

contract 属性指定终结点公开哪个协定。The contract attribute specifies which contract the endpoint is exposing. 此值对应于 ConfigurationNameServiceContractAttributeThis value maps to the ConfigurationName of the ServiceContractAttribute. 默认值为实现相应服务的类的完整类型名。The default value is the full type name of the class that implements the service.

配置元数据Configuring Metadata

<metadata>元素用于指定用于注册元数据导入扩展的设置。The <metadata> element is used to specify settings used to register metadata import extensions. 有关扩展元数据系统的详细信息,请参阅扩展元数据系统For more information about extending the metadata system, see Extending the Metadata System.

请参阅See also