共用方式為


HOW TO:設定基本 Windows Communication Foundation 用戶端

這是在建立基本 Windows Communication Foundation (WCF) 服務,及可以呼叫該服務的用戶端時,必須進行的六個工作中的第五個。如需這六個工作的概觀,請參閱使用者入門教學課程主題。

本主題會將使用Service Model Metadata Utility (Svcutil.exe) 所產生的用戶端組態檔新增至用戶端專案,並說明用戶端組態項目的內容。設定用戶端包含指定用戶端用來存取服務的端點。端點內含位址、繫結和合約,每一項都必須在設定用戶端的過程中加以指定。

針對用戶端所產生的組態檔內容將於本程序之後的範例中提供。

若要設定 Windows Communication Foundation 用戶端

  1. 請將在之前的 HOW TO:建立 Windows Communication Foundation 用戶端 程序中產生的 App.config 組態檔新增至 Visual Studio 中的用戶端專案。以滑鼠右鍵按一下 [方案總管] 中的用戶端專案,然後選取 [加入],再選取 [現有項目]。接著從下列位置選取 App.config 組態檔:C:\Users\<user name>\Documents\Visual Studio 2005\Projects\Service\Client 目錄 (這個檔案名為 App.config 檔,因為您在使用 Svcutil.exe 工具產生這個檔案時是使用 /config:app.config 參數)。按一下 [確定]。根據預設,[加入現有項目] 對話方塊會篩選出副檔名為 .config 的所有檔案。若要查看這些檔案,請從 [加入現有項目] 對話方塊右下角的下拉式清單方塊中,選取 [所有檔案 (*.*)]。

  2. 開啟產生的組態檔。Svcutil.exe 會針對繫結上的所有設定產生值。下列範例是產生之組態檔的外觀。在 <system.serviceModel> 區段下方,尋找 <endpoint> 項目。下列組態檔是所產生的檔案的簡化版本。

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.serviceModel>
        <bindings>
          <wsHttpBinding>
            <binding name="WSHttpBinding_ICalculator">
            </binding>
          </wsHttpBinding>
        </bindings>
        <client>
          <endpoint
               address="https://localhost:8000/ServiceModelSamples/Service/CalculatorService"
               binding="wsHttpBinding"
               bindingConfiguration="WSHttpBinding_ICalculator"
               contract="Microsoft.ServiceModel.Samples.ICalculator"
               name="WSHttpBinding_ICalculator">
          </endpoint>
        </client>
      </system.serviceModel>
    </configuration> 
    

    此範例會設定用戶端用來存取服務的端點,而該服務則位於下列位址:https://localhost:8000/ServiceModelSamples/service

    端點項目會指定 Microsoft.ServiceModel.Samples.ICalculator 合約用於通訊,它是使用系統提供的 WsHttpBinding 來設定的。此繫結會將 HTTP 指定為傳輸、互通安全性,與其他組態詳細資料。

  3. 如需 如何使用產生的用戶端來搭配此組態的詳細資訊,請參閱 HOW TO:使用 Windows Communication Foundation 用戶端

範例

此範例顯示針對用戶端產生的組態檔內容。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ICalculator"     
          closeTimeout="00:01:00"
          openTimeout="00:01:00" 
          receiveTimeout="00:10:00" 
          sendTimeout="00:01:00"
          bypassProxyOnLocal="false" 
          transactionFlow="false"  
          hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288" 
          maxReceivedMessageSize="65536"
          messageEncoding="Text" 
          textEncoding="utf-8" 
          useDefaultWebProxy="true"
          allowCookies="false">
          <readerQuotas maxDepth="32" 
            maxStringContentLength="8192" 
            maxArrayLength="16384"
            maxBytesPerRead="4096" 
            maxNameTableCharCount="16384" />
          <reliableSession ordered="true" 
            inactivityTimeout="00:10:00"
            enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="Windows" 
              proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="Windows" 
              negotiateServiceCredential="true"
              algorithmSuite="Default" 
              establishSecurityContext="true" />
           </security>
      </binding>
    </wsHttpBinding>
  </bindings>
  <client>
    <endpoint address="https://localhost:8000/ServiceModelSamples/Service/CalculatorService"
      binding="wsHttpBinding" 
      bindingConfiguration="WSHttpBinding_ICalculator"
      contract="ICalculator" 
      name="WSHttpBinding_ICalculator">
        <identity>
          <userPrincipalName value="user@contoso.com" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

現在,用戶端已完成設定。繼續進行 HOW TO:使用 Windows Communication Foundation 用戶端。如需疑難排解的詳細資訊,請參閱使用者入門教學課程疑難排解

請參閱

工作

HOW TO:建立 Windows Communication Foundation 用戶端

概念

使用繫結來設定服務和用戶端

其他資源

Service Metadata Utility (Svcutil.exe)
Getting Started Sample
Self-Host