共用方式為


使用組態檔設定服務

使用組態檔來設定 Windows Communication Foundation (WCF) 服務可讓您在部署時 (而不是在設計階段) 彈性提供端點與服務行為資料。本主題概要說明可用的主要技巧。

WCF 服務可使用 .NET Framework 組態技術來設定。最常見的是,會將 XML 項目新增至裝載 WCF 服務的網際網路資訊服務 (IIS) 網站的 Web.config 檔案。這些項目允許您變更詳細資料,例如各電腦的端點位址 (用於與服務通訊的實際位址)。此外,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>
ms733932.note(zh-tw,VS.90).gif注意:
繫結和行為區段都是選用的,而且只會在必要時才納入。

<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 屬性所指定的 Common Language Runtime (CLR) 型別所實作。
  • <endpoint> element reference

<bindings> 項目

bindings 項目包含所有繫結的規格,在任何服務中定義的任何端點都可以使用這些繫結。

<bindings> element reference

<binding> 項目

binding 項目 (內含在 bindings 項目) 可以是其中一個系統提供繫結 (請參閱系統提供的繫結) 或是自訂繫結 (請參閱自訂繫結)。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>

bindingConfigurationname 會在 <binding> 項目中設定。name 必須是繫結型別範圍內的唯一字串,在此情況下為 <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>