用戶端設定

您可以使用 Windows Communication Foundation (WCF) 用戶端組態來指定位址、繫結、行為及合約 (亦即用戶端端點的 "ABC" 屬性,用戶端會使用這些屬性來連線到服務端點)。 <client> 元素具有 <endpoint> 元素,其屬性可用來設定端點 ABC。 設定端點一節將討論這些屬性。

<endpoint> 元素還包含 <metadata> 元素 (可用來指定匯入和匯出中繼資料的設定)、<headers> 元素 (包含自訂位址標頭的集合),以及 <identity> 元素 (讓交換訊息的其他端點可以驗證此端點)。 <headers><identity> 元素屬於 EndpointAddress,並會在位址一文中討論。 設定中繼資料一節提供中繼資料延伸模組使用方式的說明主題連結。

設定端點

用戶端組態的設計允許用戶端指定一或多個端點,其中每個端點都有自己的名稱、位址及合約,且每個都會參考用戶端組態中的 <bindings><behaviors> 元素,以便用來設定該端點。 用戶端組態檔應該命名為 "App.config",因為這是 WCF 執行階段所預期的名稱。 下列範例示範用戶端組態檔。

<?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 屬性會針對指定的合約唯一識別端點。 ChannelFactory<TChannel>ClientBase<TChannel> 會使用此端點來指定目前已指向用戶端組態中的哪個端點,並在建立提供服務的通道時,指定必須載入的端點。 萬用字元端點組態名稱 "*" 可供使用,並指示 ApplyConfiguration 方法應該將任何端點組態載入檔案中 (前提是剛好有一個端點組態可以使用),否則將擲回例外狀況。 如果省略這個屬性,就會使用對應端點做為與所指定合約類型相關聯的預設端點。 name 屬性的預設值是空字串 (與任何其他名稱相符)。

每個端點都必須具有與其相關聯的位址,以便找出並識別端點。 address 屬性可以用來指定 URL 以提供端點的位置。 但是,您也可以使用其中一個 ServiceHost 方法,建立統一資源識別元 (URI) 並新增至 AddServiceEndpoint,以便在程式碼中指定服務端點的位址。 如需詳細資訊,請參閱位址。 如同簡介中所述,<headers><identity> 元素都屬於 EndpointAddress,並會在位址主題中討論。

binding 屬性代表當端點連接至服務時,預期使用的繫結型別。 型別必須具有註冊的組態區段,才能加以參考。 在上述範例中,這會是 <wsHttpBinding> 區段,表示端點使用 WSHttpBinding。 不過,端點可以使用的特定繫結型別可能不只一種。 其中每個在 (繫結) 類型元素中都有自己的 <binding> 元素。 bindingconfiguration 屬性可用來區分型別相同的繫結。 其值與 <binding> 元素的 name 屬性相符。 如需如何使用組態設定用戶端繫結的詳細資訊,請參閱操作說明:在組態中指定用戶端繫結

behaviorConfiguration 屬性可用來指定端點應該使用之 <endpointBehaviors><behavior>。 其值與 <behavior> 元素的 name 屬性相符。 如需使用組態來指定用戶端行為的範例,請參閱設定用戶端行為

contract 屬性會指定端點所公開的合約。 這個值會對應至 ConfigurationNameServiceContractAttribute。 預設值是實作服務之類別的完整型別名稱。

設定中繼資料

<metadata> 元素可用來指定註冊中繼資料匯入延伸模組時所使用的設定。 如需擴充中繼資料系統的詳細資訊,請參閱擴充中繼資料系統

另請參閱