使用配置文件配置服务

通过使用配置文件配置 Windows Communication Foundation (WCF) 服务,可提供在部署时而非设计时提供终结点和服务行为数据的灵活性。本主题概述了当前可用的主要技术。

可使用 .NET Framework 配置技术对 WCF 服务进行配置。通常情况下,向承载 WCF 服务的 Internet 信息服务 (IIS) 网站的 Web.config 文件添加 XML 元素。通过这些元素,可以逐台计算机更改详细信息,例如终结点地址(用于与服务进行通信的实际地址)。此外,WCF 包括几个系统提供的元素,可用于快速选择服务的最基本的功能。实际上,编写配置是 WCF 应用程序编程的主要部分。

有关更多信息,请参见 为 Windows Communication Foundation 服务配置绑定. 有关最常使用的元素的列表,请参见系统提供的绑定

System.Configuration:Web.config 和 App.config

WCF 使用 .NET Framework 的 System.Configuration 配置系统。

在 Visual Studio 中配置服务时,使用 Web.config 文件或 App.config 文件指定设置。配置文件名称的选择由为服务选择的宿主环境确定。如果正在使用 IIS 来承载服务,则使用 Web.config 文件。如果正在使用任何其他宿主环境,则使用 App.config 文件。

在 Visual Studio 中,名为 App.config 的文件可用于创建最终的配置文件。实际用于配置的最终名称取决于程序集名称。例如,名为“Cohowinery.exe”的程序集具有的最终配置文件名称为“Cohowinery.exe.config”。但是,只需要修改 App.config 文件。在编译时,对该文件所做的更改会自动应用于最终应用程序配置文件。

在使用 App.config 文件的过程中,当应用程序启动并应用配置时,文件配置系统会将 App.config 文件与 Machine.config 文件的内容合并。此机制允许在 Machine.config 文件中定义计算机范围的设置。可以使用 App.config 文件重写 Machine.config 文件的设置;也可以锁定 Machine.config 文件中的设置以应用它们。对于 Web.config,配置系统会将应用程序目录之下的所有目录中的 Web.config 文件合并到要应用的配置中。有关 配置和设置属性的更多信息,请参见 System.Configuration 命名空间中的主题。

配置文件的主要部分

配置文件中的主要部分包括以下元素。

<system.ServiceModel>

   <services>
      <service>
         <endpoint/>
      </service>
   </services>

   <bindings>
   <!-- Specify one or more of the system-provided binding elements,
    for example, <basicHttpBinding> --> 
   <!-- Alternatively, <customBinding> elements. -->
      <binding>
      <!-- For example, a <BasicHttpBinding> element. -->
      </binding>
   </bindings>

   <behaviors>
   <!-- One or more of the system-provided or custom behavior elements. -->
      <behavior>
      <!-- For example, a <throttling> element. -->
      </behavior>
   </behaviors>

</system.ServiceModel>

提示

绑定部分和行为部分是可选的,只在需要时才包括。

<services> 元素

services 元素包含应用程序承载的所有服务的规范。

<services> element reference

<service> 元素

每个服务都具有以下属性:

  • name. 指定提供服务协定的实现的类型。这是完全限定名称(命名空间和类型名称)。
  • behaviorConfiguration. 指定一个在 behaviors 元素中找到的 behavior 元素的名称。指定的行为控制操作,例如服务是否允许模拟。
  • <service> element reference

<endpoint> 元素

每个终结点都需要以下属性表示的地址、绑定和协定:

  • address. 指定服务的统一资源标识符 (URI),它可以是一个绝对地址,或是一个相对于服务基址给定的地址。如果设置为空字符串,则指示在创建服务的 ServiceHost 时,终结点在指定的基址上可用。
  • binding. 通常,指定一个类似 WsHttpBinding 的系统提供的绑定,但也可以指定一个用户定义的绑定。指定的绑定确定传输协议类型、安全和使用的编码,以及是否支持或启用可靠会话、事务或流。
  • bindingConfiguration. 如果必须修改绑定的默认值,则可通过在 bindings 元素中配置相应的 binding 元素来执行此操作。此属性应赋予与用于更改默认值的 binding 元素的 name 属性相同的值。
  • contract. 指定定义协定的接口。这是在由 service 元素的 name 属性指定的公共语言运行库 (CLR) 类型中实现的接口。
  • <endpoint> element reference

<bindings> 元素

bindings 元素包含可由任何服务中定义的任何终结点使用的所有绑定的规范。

<bindings> element reference

<binding> 元素

bindings 元素中包含的 binding 元素可以是系统提供的绑定之一(请参见系统提供的绑定),也可以是自定义绑定(请参见自定义绑定)。binding 元素具有 name 属性,此属性将绑定与 endpoint 元素的 bindingConfiguration 属性中指定的终结点相关联。

有关 配置服务和客户端的更多信息,请参见配置 Windows Communication Foundation 应用程序

<binding> element reference

<behaviors> 元素

这是定义服务行为的 behavior 元素的容器元素。

<behaviors> element reference

<behavior> 元素

每个 behavior 元素均由 name 属性进行标识,并提供系统提供的行为(例如 <throttling>)或自定义行为。

<behavior> element reference

如何使用绑定和行为配置

在 WCF 中,通过在配置中使用引用系统,可以很方便地在终结点之间共享配置。与绑定相关的配置值在 <binding> 部分的 bindingConfiguration 元素中进行分组,而不是直接将配置值分配到终结点。绑定配置是一组命名的绑定设置。然后,终结点可以通过名称来引用 bindingConfiguration

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.serviceModel>
  <bindings>
    <basicHttpBinding>
     <binding name="myBindingConfiguration1" closeTimeout="00:01:00" />
     <binding name="myBindingConfiguration2" closeTimeout="00:02:00" />
    </basicHttpBinding>
     </bindings>
     <services>
      <service name="myServiceType">
       <endpoint 
          address="myAddress" binding="basicHttpBinding" 
          bindingConfiguration="myBindingConfiguration1" />
       </service>
      </services>
    </system.serviceModel>
</configuration>

<binding> 元素中设置 bindingConfigurationnamename 必须是绑定类型(在本例中为 <basicHttpBinding>)的范围内具有唯一性的字符串。通过将 bindingConfiguration 属性设置为此字符串,终结点链接到该配置。

以相同方式实现 behaviorConfiguration,如以下示例中所示。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="myBehavior">
           <callbackDebug includeExceptionDetailInFaults="true" />
         </behavior>
       </endpointBehaviors>
    </behaviors>
    <services>
     <service name="NewServiceType">
       <endpoint 
          address="myAddress" behaviorConfiguration="myBehavior"
          binding="basicHttpBinding" />
      </service>
    </services>
   </system.serviceModel>
</configuration>

此系统允许终结点共享公共配置而不用重新定义设置。如果需要计算机范围,则在 Machine.config 中创建绑定或行为配置。配置设置在所有 App.config 文件中可用。通过Configuration Editor Tool (SvcConfigEditor.exe) 可以很方便地创建配置。

另请参见

其他资源

配置 Windows Communication Foundation 应用程序
<service>
<binding>