简化配置Simplified Configuration

配置 Windows Communication Foundation (WCF)服务可以是一种复杂的任务。Configuring Windows Communication Foundation (WCF) services can be a complex task. 该任务涉及多个不同选项,并且有时会很难确定需要哪些设置。There are many different options and it is not always easy to determine what settings are required. 虽然配置文件提高了 WCF 服务的灵活性,但是它们也是很难找到问题的源。While configuration files increase the flexibility of WCF services, they also are the source for many hard to find problems. .NET Framework 4.6.1.NET Framework 4.6.1解决了这些问题,并向用户提供了一种减小服务配置大小和降低复杂性的方法。addresses these problems and provides a way to reduce the size and complexity of service configuration.

简化配置Simplified Configuration

在 WCF 服务配置文件中,< system.serviceModel > 部分包含每个托管服务的 < service > 元素。In WCF service configuration files, the <system.serviceModel> section contains a <service> element for each service hosted. service> 元素包含 <endpoint元素的集合,这些元素指定为每项服务公开的终结点以及一组服务行为(可选)。The <service> element contains a collection of <endpoint> elements that specify the endpoints exposed for each service and optionally a set of service behaviors. <endpoint> 元素指定终结点公开的地址、绑定和协定,以及绑定配置和终结点行为(可选)。The <endpoint> elements specify the address, binding, and contract exposed by the endpoint, and optionally binding configuration and endpoint behaviors. "<system.serviceModel>" 部分还包含 <behaviors元素,可用于指定服务或终结点行为。The <system.serviceModel> section also contains a <behaviors> element that allows you to specify service or endpoint behaviors. 下面的示例演示配置文件的 < system.serviceModel > 部分。The following example shows the <system.serviceModel> section of a configuration file.

<system.serviceModel>  
  <behaviors>  
    <serviceBehaviors>  
      <behavior name="MyServiceBehavior">  
        <serviceMetadata httpGetEnabled="true">  
        <serviceDebug includeExceptionDetailInFaults="false">  
      </behavior>  
    </serviceBehaviors>  
  </behaviors>  
  <bindings>  
   <basicHttpBinding>  
      <binding name=MyBindingConfig"  
               maxBufferSize="100"  
               maxReceiveBufferSize="100" />  
   </basicHttpBinding>  
   </bindings>   <services>  
    <service behaviorConfiguration="MyServiceBehavior"  
             name="MyService">  
      <endpoint address=""  
                binding="basicHttpBinding"  
                contract="ICalculator"  
                bindingConfiguration="MyBindingConfig" />  
      <endpoint address="mex"  
                binding="mexHttpBinding"  
                contract="IMetadataExchange"/>  
    </service>  
  </services>  
</system.serviceModel>  

.NET Framework 4.6.1.NET Framework 4.6.1 通过 <service> 元素删除要求,使 WCF 服务配置更容易。makes configuring a WCF service easier by removing the requirement for the <service> element. 如果未 < service > 部分添加或在 < service > 部分中添加任何终结点,并且服务未以编程方式定义任何终结点,则会将一组默认终结点自动添加到服务中,每个服务基址对应一个终结点。和服务实现的每个协定。If you do not add a <service> section or add any endpoints in a <service> section and your service does not programmatically define any endpoints, then a set of default endpoints are automatically added to your service, one for each service base address and for each contract implemented by your service. 在上述每个终结点中,终结点地址与基址相对应,绑定由基址方案确定,协定即为服务实现的协定。In each of these endpoints, the endpoint address corresponds to the base address, the binding is determined by the base address scheme and the contract is the one implemented by your service. 如果你不需要指定任何终结点或服务行为,或者不需要更改任何绑定设置,则完全不必指定服务配置文件。If you do not need to specify any endpoints or service behaviors or make any binding setting changes, you do not need to specify a service configuration file at all. 如果服务实现了两个协定,并且主机同时启用了 HTTP 和 TCP 传输,服务主机将创建四个默认终结点,使用每个传输的每一协定各对应一个终结点。If a service implements two contracts and the host enables both HTTP and TCP transports the service host creates four default endpoints, one for each contract using each transport. 若要创建默认终结点,服务主机必须了解要使用的绑定。To create default endpoints the service host must know what bindings to use. 这些设置在 "< system.serviceModel >" 部分的 < protocolMappings > "部分中指定。These settings are specified in a <protocolMappings> section within the <system.serviceModel> section. "<protocolMappings>" 部分包含映射到绑定类型的传输协议方案的列表。The <protocolMappings> section contains a list of transport protocol schemes mapped to binding types. 服务主机使用传递到它的基址来确定要使用的绑定。The service host uses the base addresses passed to it to determine which binding to use. 下面的示例使用 < protocolMappings > 元素。The following example uses the <protocolMappings> element.

警告

更改默认配置元素(如绑定或行为)可能会影响在配置层次结构的较低级别中定义的服务,因为这些服务可能使用这些默认绑定和行为。Changing default configuration elements, such as bindings or behaviors, might affect services defined in lower levels of the configuration hierarchy, since they might be using these default bindings and behaviors. 因而,更改默认绑定和行为的任何人员都需要注意,这些更改可能会影响层次结构中的其他服务。Thus, whoever changes default bindings and behaviors needs to be aware that these changes might affect other services in the hierarchy.

备注

承载于 Internet 信息服务 (IIS) 或 Windows 进程激活服务 (WAS) 下的服务使用虚拟目录作为其基址。Services hosted under Internet Information Services (IIS) or Windows Process Activation Service (WAS) use the virtual directory as their base address.

<protocolMapping>  
  <add scheme="http"     binding="basicHttpBinding" bindingConfiguration="MyBindingConfiguration"/>  
  <add scheme="net.tcp"  binding="netTcpBinding"/>  
  <add scheme="net.pipe" binding="netNamedPipeBinding"/>  
  <add scheme="net.msmq" binding="netMSMQBinding"/>  
</protocolMapping>  

在上一示例中,基址以“http”方案开头的终结点使用 BasicHttpBindingIn the previous example, an endpoint with a base address that starts with the "http" scheme uses the BasicHttpBinding. 基址以“net.tcp”方案开头的终结点使用 NetTcpBindingAn endpoint with a base address that starts with the "net.tcp" scheme uses the NetTcpBinding. 你可以在本地 App.config 或 Web.config 文件中重写相应设置。You can override settings in a local App.config or Web.config file.

"<protocolMappings>" 部分中的每个元素必须指定方案和绑定。Each element within the <protocolMappings> section must specify a scheme and a binding. 还可以指定一个 bindingConfiguration 属性,该属性在配置文件的 < bindings > 部分中指定绑定配置。Optionally it can specify a bindingConfiguration attribute that specifies a binding configuration within the <bindings> section of the configuration file. 如果未指定 bindingConfiguration,则使用相应绑定类型的匿名绑定配置。If no bindingConfiguration is specified, the anonymous binding configuration of the appropriate binding type is used.

使用匿名 < 为默认终结点配置服务行为 behavior < serviceBehaviors > 部分中 > 部分。Service behaviors are configured for the default endpoints by using anonymous <behavior> sections within <serviceBehaviors> sections. serviceBehaviors> 中的任何未命名 <behavior> 元素用于配置服务行为。Any unnamed <behavior> elements within <serviceBehaviors> are used to configure service behaviors. 例如,下面的配置文件对主机中的所有服务启用服务元数据发布。For example, the following configuration file enables service metadata publishing for all services within the host.

<system.serviceModel>  
    <behaviors>  
      <serviceBehaviors>  
        <behavior>  
          <serviceMetadata httpGetEnabled="True"/>  
        </behavior>  
      </serviceBehaviors>  
    </behaviors>    <!-- No <service> tag is necessary. Default endpoints are added to the service -->  
    <!-- The service behavior with name="" is picked up by the service -->  
 </system.serviceModel>  

终结点行为是使用匿名 < behavior > < serviceBehaviors > 部分内的节来配置的。Endpoint behaviors are configured by using anonymous <behavior> sections within <serviceBehaviors> sections.

下面示例中的配置文件使用简化配置模型,它等效于本主题开头的配置文件。The following example is a configuration file equivalent to the one at the beginning of this topic that uses the simplified configuration model.

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
       <basicHttpBinding>
          <binding maxBufferSize="100"
                   maxReceiveBufferSize="100" />
       </basicHttpBinding>
    </bindings>
    <!-- No <service> tag is necessary. Default endpoints will be added to the service -->
    <!-- The service behavior with name="" will be picked up by the service -->
    <protocolMapping>
      <add scheme="http" binding="basicHttpBinding" />
  </protocolMapping>
  </system.serviceModel>

重要

此功能只与 WCF 服务配置相关,与客户端配置无关。This feature relates only to WCF service configuration, not client configuration. 大多数时候,使用 svcutil.exe 之类的工具或从 Visual Studio 添加服务引用将生成 WCF 客户端配置。Most times, WCF client configuration will be generated by a tool such as svcutil.exe or adding a service reference from Visual Studio. 如果要手动配置 WCF 客户端,则需要将 <client > 元素添加到配置中,并指定希望调用的任何终结点。If you are manually configuring a WCF client you will need to add a <client> element to the configuration and specify any endpoints you wish to call.

请参阅See also