首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

WCF 系列(三)WCF 設定檔完整註解與結構詳解

     編輯:關於.NET

概述

WCF 的設定系統建立在 .NET System.Configuration 之上,核心區段為 <system.serviceModel>。透過設定檔將端點、繫結、行為等與程式碼分離,可大幅提升部署彈性與維護性。本文提供一份完整、帶詳細中文註解的範本,涵蓋服務端與用戶端所有常用設定元素。

完整設定檔範本

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <!-- services: 定義應用程式中所有裝載服務的設定 -->
    <services>
      <!-- service: 單一服務設定
           name: 實作合約之服務類別的完整型別名稱 (命名空間.類別名)
           behaviorConfiguration: 參考 <serviceBehaviors> 中某 behavior 的 name -->
      <service name="名稱空間.類型名" behaviorConfiguration="behavior名">
        <host>
          <baseAddresses>
            <!-- 每種傳輸協定僅能定義一個 baseAddress;HTTP baseAddress 同時也是服務說明頁面 URL -->
            <add baseAddress="https://address" />
          </baseAddresses>
          <timeouts></timeouts>
        </host>
        <!-- endpoint: 服務端點,一個服務可有多個端點
             address: 端點對外 URI,可為絕對或相對於 baseAddress 的相對位址;空值則等於 baseAddress
             binding: 使用的繫結 (basicHttpBinding 等 9 種系統繫結或 customBinding)
             contract: 端點實作之合約完整型別名稱
             bindingConfiguration: 參考 <bindings> 中同類繫結的 name
             behaviorConfiguration: 參考 <endpointBehaviors> 中的 name
             name: 端點名稱,同一合約下多端點名稱必須唯一 -->
        <endpoint address="URI" binding="basicHttpBinding" contract="Contract全限定名" 
                  bindingConfiguration="binding名" behaviorConfiguration="String" name="">
          <headers><!-- 自訂 SOAP Header 內容 --></headers>
        </endpoint>
      </service>
    </services>

    <!-- bindings: 定義系統預設或自訂繫結的具體參數 -->
    <bindings>
      <basicHttpBinding>
        <binding name="binding名"></binding>
      </basicHttpBinding>
    </bindings>

    <!-- behaviors: 定義服務與端點行為 -->
    <behaviors>
      <!-- serviceBehaviors: 服務層級行為 -->
      <serviceBehaviors>
        <behavior name="此Behavior名稱">
          <!-- serviceMetadata: 控制中繼資料發布
               httpGetEnabled: 是否允許 HTTP GET 取得 WSDL
               httpGetUrl: WSDL 發布位址,未指定則為 HTTP baseAddress + ?wsdl -->
          <serviceMetadata httpGetEnabled="true" httpGetUrl="https://URI:port/address" />
          <!-- serviceCredentials: 服務端憑證設定 -->
          <serviceCredentials>
            <serviceCertificate storeName="存儲區" storeLocation="存儲位置" 
                                x509FindType="FindBySubjectName" findValue="server1" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>

      <!-- endpointBehaviors: 端點層級行為 (常用於用戶端) -->
      <endpointBehaviors>
        <behavior name="此Behavior名稱">
          <clientCredentials>
            <clientCertificate storeName="存儲區" storeLocation="存儲位置" 
                               x509FindType="FindBySubjectName" findValue="Client1" />
            <serviceCertificate>
              <authentication certificateValidationMode="None" />
            </serviceCertificate>
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <!-- client: 用戶端端點設定 -->
    <client>
      <endpoint address="URI" binding="basicHttpBinding" 
                bindingConfiguration="binding名" behaviorConfiguration="String"
                contract="Contract全限定名" name="endpoint配置名">
        <identity>
          <!-- 用戶端驗證服務身分,擇一使用 -->
          <userPrincipalName></userPrincipalName>
          <servicePrincipalName></servicePrincipalName>
          <dns></dns>
          <rsa></rsa>
          <certificate encodedValue="" />
          <certificateReference storeName="存儲區" storeLocation="存儲位置" 
                                x509FindType="FindBySubjectName" findValue="Client1" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

關鍵元素說明

<services> / <service>

name 屬性必須與程式碼中 ServiceHost 載入的服務型別完全一致,否則設定不生效。behaviorConfiguration 連結至 <serviceBehaviors> 中的行為定義。

<host> / <baseAddresses>

每種傳輸協定 (HTTP、HTTPS、TCP、Named Pipe 等) 僅能有一個基底位址。端點的 address 若為相對路徑,將依此基底位址組合成完整 URI。

<endpoint> (服務端)

ABC 三要素:Address (位址)、Binding (繫結)、Contract (合約)。bindingConfiguration 允許同一繫結類型有多組參數設定 (如不同安全模式)。

<bindings>

系統提供 9 種標準繫結:basicHttpBinding、wsHttpBinding、netTcpBinding、netNamedPipeBinding、netMsmqBinding、wsFederationHttpBinding、wsDualHttpBinding、netPeerTcpBinding、msmqIntegrationBinding。各繫結下可定義多個 <binding name="..."> 供端點參考。

<behaviors>

  • <serviceBehaviors>:服務層級,如中繼資料發布、授權、節流、錯誤處理。
  • <endpointBehaviors>:端點層級,常用於用戶端驗證、序列化自訂。

<client> / <endpoint> (用戶端)

name 屬性對應用戶端 Proxy 建構子的 endpointConfigurationName 參數。若該合約僅有一個端點設定,可使用無參數建構子自動匹配。

實務提示

  • 設定檔路徑:Console/WinForm 為 App.config,IIS 裝載為 Web.config。
  • 修改設定檔後無需重新編譯,重新啟動服務即可生效 (IIS 需回收應用程式集區)。
  • 生產環境建議關閉 serviceMetadata httpGetEnabled,避免洩露合約細節。
  • 憑證設定中 storeLocation 通常為 LocalMachine (服務端) 或 CurrentUser (用戶端)。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved