[WCF 學習筆記] 9. 端點綁定

來源:互聯網
上載者:User

Endpoints 包含了重要的地址資訊,包括服務地址以及通訊協議,用戶端只有獲知這些資訊才能與服務建立通訊。而 Binding
用於指定 WCF 兩個端點(endpoint)之間的通訊細節,兩個端點的 Binding
應該是一致的。端點和綁定對象合作,共同完成服務通訊和調用。服務面對的用戶端是多樣的,不同的用戶端可能選擇不同的通訊方式,不同的安全方案。WCF
允許我們為服務添加多個綁定和端點,這樣我們就可以使用同一個服務為不同的客戶需求提供解決方案,這是 WebServices 和 Remoting
所無法完成的。

Endpoints 格式:scheme://machine:port/path

如:
http://192.168.0.1:8080/IService1
net.tcp://202.100.100.1:123/IService2
net.pipe://localhost/MyPipe
net.msmq://localhost/MyService

Binding 的內容包括:

  • Protocols: 設定安全機制。如訊息可靠接收,事務上下文等。
  • Encoding: 設定訊息編碼方式。如 Text/XML、Binary 或 MTOM。
  • Transport: 設定底層通訊協議。如 Http、TCP 等。

Binding
由一系列分工合作的 Element 組成,其中至少包括用於通訊和訊息編碼的兩個 Element。WCF Framework
為我們提供了大量常用的 Binding,當然我們也可以自己開發專用的 Binding 類型。使用 Binding
很簡單,建立系統提供或自己開發的 Binding
對象,調整或使用其預設屬性,最後用該綁定對象作為參數建立端點即可。我們可以使用編碼或者設定檔來調整綁定對象的屬性,有關設定檔的資訊可以參考
MSDN 或者雨痕後面撰寫的章節。

WCF Framework 提供的 Binding

  • BasicHttpBinding: 最簡單的綁定類型,通常用於 Web Services。使用 HTTP 協議,Text/XML 編碼方式。
  • WSHttpBinding: 比 BasicHttpBinding 更加安全,通常用於 non-duplex 服務通訊。
  • WSDualHttpBinding: 和 WSHttpBinding 相比,它支援 duplex 類型的服務。
  • WSFederationHttpBinding: 支援 WS-Federation 安全通訊協議。
  • NetTcpBinding: 效率最高,安全的跨機器通訊方式。
  • NetNamedPipeBinding: 安全、可靠、高效的單機服務通訊方式。
  • NetMsmqBinding: 使用訊息佇列在不同機器間進行通訊。
  • NetPeerTcpBinding: 使用 P2P 協議在多機器間通訊。
  • MsmqIntegrationBinding: 使用現有的訊息佇列系統進行跨機器通訊。如 MSMQ。

Binding 基本資料

粗體表示其基本編碼方式。我們注意到 NetTcpBinding、NetPeerTcpBinding、NetNamedPipeBinding、NetMsmqBinding 只能用於 .NET WCF-to-WCF。
(圖片選自 《Programming WCF Services》)

Binding 選擇策略

(圖片選自 《Programming WCF Services》)

有關 Binding 的更詳細資料請參考 ms-help://MS.MSSDK.1033/MS.NETFX30SDK.1033/WCF_con/html/2c243746-45ce-4588-995e-c17126a579a6.htm

Example

Server.cs

ServiceHost host = new ServiceHost(typeof(CalculateService));
host.AddServiceEndpoint(typeof(ICalculate), new WSHttpBinding(), "http://localhost:8080/calc");
host.Open();

Client.cs

CalculateClient client = new CalculateClient(new WSHttpBinding(), new EndpointAddress("http://localhost:8080/calc"));
client.Add(1, 2);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.