For faster navigation, this Iframe is preloading the Wikiwand page for Windows通訊基礎.

Windows通訊基礎

此條目已列出參考文獻,但因為沒有文內引註而使來源仍然不明。 (2023年6月8日)请加上合适的文內引註来改善这篇条目

Windows通訊基礎(英語:Windows Communication Foundation, WCF)是由微軟發展的一組資料通訊的應用程式開發介面,它是.NET框架的一部分,由.NET Framework 3.0开始引入,與Windows Presentation FoundationWindows Workflow Foundation並列為新一代Windows作業系統以及WinFX的三個重大應用程式開發類別庫。

在.NET Framework 2.0以及前版本中,微軟發展了Web Service(SOAP with HTTP communication),.NET Remoting(TCP/HTTP/Pipeline communication)以及基礎的Winsock等通訊支援,由於各個通訊方法的設計方法不同,而且彼此之間也有相互的重疊性(例如.NET Remoting可以開發SOAP, HTTP通訊),對於開發人員來說,不同的選擇會有不同的程式設計模型,而且必須要重新學習,讓開發人員在使用时有許多不便。同時,服務導向架構(Service-Oriented Architecture)也開始盛行於軟體工業中,因此微軟重新檢視了這些通訊方法,並設計了一個統一的程式開發模型,對於資料通訊提供了最基本最有彈性的支援,這就是Windows Communication Foundation。

概念

WCF由於集合了幾乎由.NET Framework所提供的通訊方法,因此學習曲線比較陡峭,開發人員必須要針對各個部份的內涵做深入的了解,才能夠操控WCF來開發應用程式。

  • 通訊雙方的溝通方式,由合約 (Contract)來訂定。
  • 通訊雙方所遵循的通訊方法 (communication protocol),由協定繫結 (Binding)來訂定。
  • 通訊期間的安全性,由雙方約定的安全性層次來訂定。

合約(Contract)

WCF的基本概念是以合約(Contract)來定義雙方溝通的協定,合約必須要以介面的方式來呈現,而實際的服務程式碼必須要由這些合約介面衍生並實作。合約分成了四種:

  1. 資料合約(Data Contract),訂定雙方溝通時的資料格式。
  2. 服務合約(Service Contract),訂定服務的定義。
  3. 營運合約(Operation Contract),訂定服務提供的方法。
  4. 訊息合約(Message Contract),訂定在通訊期間覆寫訊息內容的規範。

一個WCF中的合約,就如同下列程式碼所示:

using System;
using System.ServiceModel;
namespace Microsoft.ServiceModel.Samples
{
  [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")] // 服務合約
  public interface ICalculator
  {
    [OperationContract] // 營運合約
    double Adddouble n1, double n2;
    [OperationContract] // 營運合約
    double Subtractdouble n1, double n2;
    [OperationContract] // 營運合約
    double Multiplydouble n1, double n2;
    [OperationContract] // 營運合約
    double Dividedouble n1, double n2;
  }
}

協定繫結(Binding)

由於WCF支援了HTTPTCP命名管道MSMQ、Peer-To-Peer TCP等協定,而HTTP又分為基本HTTP支援(BasicHttpBinding)以及WS-HTTP支援(WsHttpBinding),而TCP亦支援NetTcpBinding,NetPeerTcpBinding等通訊方式,因此,雙方必須要統一通訊的協定,並且也要在編碼以及格式上要有所一致。

一個設定通訊協定繫結的範例如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <!-- 設定服務繫結的資訊 -->
    <services>
      <service name=" CalculatorService" >
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="Binding1"
            contract="ICalculator" />
      </service>
    </services>
    <!-- 設定通訊協定繫結的資訊 -->
    <bindings>
      <wsHttpBinding>
        <binding name="Binding1">
        </binding>
      </wsHttpBinding>
   </bindings>
  </system.serviceModel>
</configuration>

雖然WCF也可以使用SOAP做通訊格式,但它和以往的ASP.NET XML Web Services不同,因此有部份技術文章中,會將ASP.NET的XML Web Services稱為ASMX Service

WCF的服務可以掛載於Console Application,Windows Application,IIS(ASP.NET)Application,Windows Service以及Windows Activation Services中,但大多都會掛在Windows Service。

安全性層次

WCF實作上已經支援了傳輸層次安全性(Transport-level security)以及訊息層次安全性(Message-level security)兩種。

  • 傳輸層次安全性:在資料傳輸時期加密,例如SSL
  • 訊息層次安全性:在資料處理時就加密,例如使用數位簽章雜湊或是使用金鑰加密法等。

用戶端

對於WCF的用戶端來說,WCF服務就像是一個Web Service一樣,在Visual Studio 2008中,所有WCF服務的連接都是由用戶端的WCF Service Proxy來執行,開發人員不用花費太多心思在通訊上,而WCF Service Proxy在Visual Studio中被稱為服務參考(Service Reference)。

在Visual Studio中加入WCF的服務參考時,Visual Studio會自動幫開發人員做掉一些必要工作(例如組態建立以及產生Service Proxy等),開發人員只需要在程式碼中取用WCF Service Proxy物件即可。

参考文献

{{bottomLinkPreText}} {{bottomLinkText}}
Windows通訊基礎
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?