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

WCF 系列(二)使用設定檔建置與呼叫 WCF 服務完整實作

     編輯:關於.NET

前言

承接上一篇「完全不使用設定檔」的程式碼式建置方式,本文示範如何將位址、繫結、行為等可變因素移至設定檔,達成程式碼與設定分離,提升彈性與維護性。

一、服務端實作

1.1 定義合約與服務實作 (不變)


[ServiceContract]
public interface IService
{
    [OperationContract]
    string MyOperation1(string myValue);
}

public class Service : IService
{
    public string MyOperation1(string myValue)
    {
        return "Hello: " + myValue;
    }
}

1.2 建立 ServiceHost (簡化版)


// 僅指定服務型別,BaseAddress 移至設定檔
ServiceHost host = new ServiceHost(typeof(Service));
host.Open();

1.3 設定檔 (App.config / Web.config)


<configuration>
  <system.serviceModel>
    <services>
      <service name="WCFService.Service" behaviorConfiguration="NewBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost:8080/WCFService/Service" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" contract="WCFService.IService" />
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="NewBehavior">
          <serviceMetadata httpGetEnabled="true" httpGetUrl="https://localhost:8001/" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

重點:<service name> 必須為服務實作類別的完整型別名;behaviorConfiguration 連結至啟用中繼資料發布的行為;端點 address="" 表示直接使用 baseAddress。

1.4 啟用中繼資料發布

透過 <serviceMetadata httpGetEnabled="true"> 讓服務公開 WSDL,方便用戶端產生 Proxy。httpGetUrl 可自訂 WSDL 發布位址,未指定則預設為 HTTP baseAddress + ?wsdl。

二、用戶端實作

2.1 加入服務參考

在 Visual Studio 專案右鍵選擇「加入服務參考」,輸入服務的 HTTP baseAddress (如 https://localhost:8080/WCFService/Service),VS 會自動下載中繼資料並產生:

  • Proxy 類別 (繼承 ClientBase<T>)
  • 用戶端設定檔區段 (<client>)

加入服務參考對話框

2.2 產生的用戶端設定檔


<client>
  <endpoint address="https://localhost:8080/WCFService/Service"
            binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService"
            contract="WCFClient.localhost.IService"
            name="BasicHttpBinding_IService" />
</client>

2.3 建立 Proxy 與呼叫服務

Proxy 類別提供多個建構子多載:

  • ServiceClient() — 自動尋找唯一對應合約的端點設定
  • ServiceClient(string endpointConfigurationName) — 指定端點設定名稱
  • ServiceClient(string endpointConfigurationName, string remoteAddress)
  • ServiceClient(string endpointConfigurationName, EndpointAddress remoteAddress)

本例僅有一個端點,故可使用最簡單建構子:


localhost.ServiceClient proxy = new localhost.ServiceClient();
string result = proxy.MyOperation1("myFirstWCF");
Console.WriteLine(result); // 輸出: Hello: myFirstWCF

三、程式碼與設定檔的對應關係

程式碼式建置設定檔式建置
new ServiceHost(typeof(Service), baseAddress)<service name="..."> + <host><baseAddresses>
host.AddServiceEndpoint(...)<endpoint ... />
host.Description.Behaviors.Add(metadataBehavior)<behavior><serviceMetadata /></behavior>
ChannelFactory<T>.CreateChannel(binding, address)new Proxy() 或 new Proxy("endpointName")

優缺點比較

維度程式碼式設定檔式
部署彈性低 (需重編譯)高 (修改設定檔即可)
可讀性邏輯集中、易追蹤分散、需對照設定檔
適用場景自我裝載、動態端點、測試專案生產環境、IIS 裝載、多環境部署

結語

設定檔式建置是企業級 WCF 應用的主流做法。下一篇將詳細解析設定檔各區段屬性與進階設定技巧。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved