萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

Windows Communication Foundation (WCF) 基礎入門與實作

Windows Communication Foundation(WCF)——即早期代號 Indigo——是微軟用於 Windows 的新一代連接式系統平台。本文介紹基本原理,並建立一個簡單的應用程式。

WCF 核心優勢

WCF 將過去所有分散的微軟連接技術——Web Services (ASMX)、Web Services Enhancements (WS*)、Microsoft Message Queuing (MSMQ)、Enterprise Services、COM+ 與 .NET Remoting——整合至單一的 System.ServiceModel 命名空間。這極大簡化了設計、開發與部署連接式應用程式的流程:開發人員無需在大量不同命名空間的實作間選擇,也無需撰寫樣板程式碼來建立連接。無論應用程式透過鬆散耦合的 Web Services 連接,還是透過緊密耦合的企業級服務連接,程式碼模型皆保持一致,不同訊息類型間的轉換也更平滑——因為它們共用同一個程式設計命名空間。

使用 WCF 的三要素

WCF 遵循「軟體即服務」模型,所有功能單元皆定義為服務。開發人員只需關注三個核心問題:

  1. 服務位址:服務在哪裡?網際網路、區域網路某台電腦、或本機?
  2. 服務繫結:如何與它對話?使用 SOAP、MSMQ 或其他協定?
  3. 服務契約:它能做什麼?需傳遞什麼資料型別?回傳什麼資料?

熟悉 Web Services 的開發者會發現這對應 WSDL 的三大要素。WCF 採用此成功模型並擴展之,使其能與其他通訊形式(MSMQ、Enterprise Services、COM+、.NET Remoting)協同運作。

實作第一個 WCF 服務

若對 Binding、Contract、Addressing 等術語不熟,無妨,從範例學習最快。請依下列步驟建立第一個 Indigo 應用程式:

前置準備

在硬碟建立實體目錄 C:\tconvert 存放應用程式,並將其對應為 IIS 虛擬目錄 Temperatures。若使用 Windows Server 2003,請確認 ASPNET 進程可存取該目錄。

步驟 1:定義服務契約

建立介面並標記 [ServiceContract] 與 [OperationContract] 屬性:

namespace Devx.Indigo.Samples
{
    using System;
    using System.ServiceModel;

    [ServiceContract()]
    public interface ITemperatures
    {
        [OperationContract]
        double ftoc(double n1);
        [OperationContract]
        double ctof(double n1);
    }
}

ServiceContract 宣告介面為 WCF 服務契約,OperationContract 宣告方法為契約中的作業。

步驟 2:實作服務類別

using System;
using System.ServiceModel;

namespace Devx.Indigo.Samples
{
    [ServiceContract()]
    public interface ITemperatures
    {
        [OperationContract]
        double ftoc(double n1);
        [OperationContract]
        double ctof(double n1);
    }

    public class TemperatureService : ITemperatures
    {
        public double ftoc(double f)
        {
            return ((f - 32) * 5) / 9;
        }

        public double ctof(double c)
        {
            return ((c + 32) * 9) / 5;
        }
    }
}

步驟 3:編譯與部署

於命令提示字元編譯:

csc /r:System.ServiceModel.dll /out:TemperatureService.dll /t:library TemperatureService.cs

在 tconvert 目錄下建立 BIN 子目錄,並複製生成的 DLL 至其中。

步驟 4:設定 web.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns="https://schemas.microsoft.com/.NetConfiguration/v2.0">
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="TemperatureServiceBehavior" type="Devx.Indigo.Samples.TemperatureService">
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="Binding1" contract="Devx.Indigo.Samples.ITemperatures" />
      </service>
    </services>
    <behaviors>
      <behavior configurationName="TemperatureServiceBehavior" returnUnknownExceptionsAsFaults="True"></behavior>
    </behaviors>
    <bindings>
      <wsHttpBinding>
        <binding configurationName="Binding1" />
      </wsHttpBinding>
    </bindings>
  </system.serviceModel>
  <system.web>
    <compilation debug="true" />
  </system.web>
</configuration>

<system.serviceModel> 區段保存 WCF 服務的所有資訊,包含服務、行為與繫結三大設定。

步驟 5:建立服務對應檔

在 tconvert 目錄建立 service.svc 文字檔,內容:

<%@Service language=c# Debug="true" class="Devx.Indigo.Samples.TemperatureService" %>

服務已建立,可透過 https://localhost/Temperatures/service.svc 存取,瀏覽器將顯示服務說明頁面。

建立客戶端應用程式

WCF SDK 提供 svcutil.exe 工具為客戶端產生 Proxy 程式碼:

svcutil /language:C# /config:app.config https://192.168.0.1/Temperatures/service.svc?wsdl

產生的 Proxy 類別預設命名空間為 tempuri.org,檔案名為 tempuri.org.cs。客戶端使用方式:

using System;
using System.ServiceModel;

namespace Devx.Indigo.Samples
{
    class TemperatureClient
    {
        static void Main()
        {
            using (TemperaturesProxy proxy = new TemperaturesProxy("default"))
            {
                double c = 22.2;
                double f = proxy.ctof(c);
                Console.WriteLine("{0} degrees C = {1} degrees F", c, f);
                f = 93.7;
                c = proxy.ftoc(f);
                Console.WriteLine("{0} degrees C = {1} degrees F", c, f);
                proxy.Close();
            }
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }
    }
}
copyright © 萬盛學電腦網 all rights reserved