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物件即可。

参考文献

Wikiwand in your browser!

Seamless Wikipedia browsing. On steroids.

Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.

Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.