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

WCF 端點總覽(二):Address、Binding、Contract 與 Behavior 詳細解析

     編輯:關於.NET

端點四大要素 (ABC+B)

WCF 通訊透過端點實現,每個端點包含四個核心元素:

  • Address (A):URI 唯一識別端點位置,解決「在哪裡找到服務」。
  • Binding (B):封裝通訊細節(編碼、傳輸、安全性等),解決「如何與服務通訊」。
  • Contract (C):暴露服務功能,將 Operation 對應為 Message Exchange Pattern (MEP),解決「服務提供什麼功能」。
  • Behavior:自訂執行期行為(逾時、認證、交易等)。

Client 端點必須與 Service 端點完全匹配(Address、Binding、Contract)才能通訊。

完整範例架構

方案包含 Contract、Service、Hosting (Self-host)、Client、IIS Host 五個專案。以下展示關鍵程式碼與設定。

Service Contract

[ServiceContract]
public interface IGeneralCalculator
{
    [OperationContract]
    double Add(double x, double y);
}

Service 實作

public class GeneralCalculatorService : IGeneralCalculator
{
    public double Add(double x, double y) { return x + y; }
}

Self-Host 程式碼與設定

Program.cs 兩種啟動方式:程式碼建立端點、或透過設定檔。

// 程式碼方式
Uri httpBase = new Uri("https://localhost:8888/generalCalculator");
Uri tcpBase = new Uri("net.tcp://localhost:9999/generalCalculator");
using (ServiceHost host = new ServiceHost(typeof(GeneralCalculatorService), httpBase, tcpBase))
{
    host.AddServiceEndpoint(typeof(IGeneralCalculator), new BasicHttpBinding(), "");
    host.AddServiceEndpoint(typeof(IGeneralCalculator), new NetTcpBinding(), "");
    // 啟用 Metadata
    ServiceMetadataBehavior behavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
    if (behavior == null) { behavior = new ServiceMetadataBehavior(); host.Description.Behaviors.Add(behavior); }
    behavior.HttpGetEnabled = true;
    host.Opened += delegate { Console.WriteLine("Calculator Service has begun to listen"); };
    host.Open();
    Console.Read();
}

App.config 設定檔方式:

<services>
  <service name="Artech.WCFService.Service.GeneralCalculatorService" behaviorConfiguration="calculatorServieBehavior">
    <endpoint address="" binding="basicHttpBinding" contract="Artech.WCFService.Contract.IGeneralCalculator" />
    <endpoint address="" binding="netTcpBinding" contract="Artech.WCFService.Contract.IGeneralCalculator" />
    <host>
      <baseAddresses>
        <add baseAddress="https://localhost:8888/generalcalculator" />
        <add baseAddress="net.tcp://localhost:9999/generalCalculator" />
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="calculatorServieBehavior">
      <serviceMetadata httpGetEnabled="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

IIS Host 設定

.svc 檔:<%@ ServiceHost Service="Artech.WCFService.Service.GeneralCalculatorService" %>

Web.config 類似 Self-host 但無 baseAddresses(由 IIS 提供)。

Client 端點設定

<client>
  <endpoint name="selfHostEndpoint_http" address="https://localhost:8888/generalCalculator" binding="basicHttpBinding" contract="Artech.WCFService.Contract.IGeneralCalculator" />
  <endpoint name="selfHostEndpoint_tcp" address="net.tcp://localhost:9999/generalCalculator" binding="netTcpBinding" contract="Artech.WCFService.Contract.IGeneralCalculator" />
  <endpoint name="iisHostEndpoint" address="https://localhost/wcfservice/GeneralCalculatorService.svc" binding="basicHttpBinding" contract="Artech.WCFService.Contract.IGeneralCalculator" />
</client>

Client 程式碼透過 ClientBase<T> 呼叫,建構函式可傳入 endpointConfigurationName、Binding+Address 等。

Address 結構與 Header 應用

Address 包含 URI (必要)、Identity (可選,用於驗證)、Headers (可選,用於路由、驗證等進階案例)。可在設定檔中加入 Headers:

<endpoint ...>
  <headers>
    <role>admin</role>
  </headers>
</endpoint>

Binding 組成層次

Binding 是 BindingElement 集合,分三層:

層級選項必要性
TransactionTransactionFlowBindingElement否
ReliabilityReliableSessionBindingElement否
SecuritySecurityBindingElement否
EncodingText, Binary, MTOM, Custom是
TransportTCP, Named Pipes, HTTP, HTTPS, MSMQ, Custom是

每個 Binding 必須包含一個 Transport 與一個 Encoding Element,Protocol Element 視需求加入。

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