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

WCF 3.5 HTTP 程式設計模型增強:REST、JSON、Syndication 整合

     編輯:關於.NET

前言

.NET Framework 3.5 的 WCF 建構於 3.0 的擴充點之上,提供一流的 Web 風格服務支援:簡潔的 HTTP 程式模型、JSON 訊息、Syndication (RSS/ATOM) 整合。這使 WCF 成為連接 Web 客戶端 (AJAX、Silverlight、瀏覽器) 的首選平台。

核心亮點

  • Web 風格通訊與 SOAP/WS-* 整合在單一服務堆疊:同一服務可對內用 SOAP/WS-*,對外用 Web 協定
  • 支援 TCP 傳輸 SOAP/WS-*、二進制訊息編碼:企業內部高效穩定通訊
  • REST 支援達到空前強大:POX、Syndication 格式

關鍵新類型與屬性

類型/屬性命名空間/組件用途
WebHttpBindingSystem.ServiceModel.WebHTTP 繫結,支援 REST
WebHttpBehaviorSystem.ServiceModel.Web端點行為,啟用 Web 程式模型
WebGetAttributeSystem.ServiceModel.Web對應 HTTP GET
WebInvokeAttributeSystem.ServiceModel.Web對應非 GET 動作 (POST/PUT/DELETE 等)
WebServiceHostSystem.ServiceModel.Web自動配置 WebHttpBinding + WebHttpBehavior
WebServiceHostFactorySystem.ServiceModel.WebIIS 裝載專用,免設定檔

REST 服務定義範例

[ServiceContract]
public interface IPictureService
{
    [OperationContract]
    [WebGet(UriTemplate = "picture/{pictureId}")]
    Stream GetPicture(string pictureId);

    [OperationContract]
    [WebGet(UriTemplate = "picture/t/{pictureId}")]
    Stream GetPictureThumbnail(string pictureId);

    [OperationContract]
    [WebInvoke(UriTemplate = "update", Method = "POST")]
    void UpdatePictureInfo(PictureInfo info);
}
  • UriTemplate:REST 風格 URL 範本,大括號內為參數
  • WebGet:唯讀操作,可被快取
  • WebInvoke:狀態變更操作,指定 Method
  • 回傳 Stream 可直接輸出圖片、檔案等二進位內容

IIS 裝載簡化:.svc + WebServiceHostFactory

<%@ ServiceHost
    Language="C#"
    Service="PictureService"
    Factory="System.ServiceModel.Web.WebServiceHostFactory" %>

web.config 完全不需要 WCF 相關設定。Factory 會自動:

  1. 建立 ServiceHost
  2. 加入 WebHttpBinding 端點
  3. 套用 WebHttpBehavior
  4. 開啟 ServiceHost

Syndication (RSS/ATOM) 整合

命名空間:System.ServiceModel.Syndication (位於 System.ServiceModel.Web.dll),不依賴 WCF 執行階段,可在任何 AppDomain 使用 (ASP.NET、WPF、Windows Service 等)。

支援格式

  • RSS 2.0
  • ATOM 1.0
  • 可擴展自訂格式

程式設計模型:避開格式細節

// 讀取 ATOM 來源,轉換為 RSS 2.0 輸出
Uri feedUri = new Uri("https://blogs.msdn.com/justinjsmith/atom.xml");
SyndicationFeed feed = SyndicationFeed.Load(feedUri);

Rss20FeedFormatter formatter = new Rss20FeedFormatter(feed);
XmlWriter writer = XmlWriter.Create(Console.Out, null);
formatter.WriteTo(writer);
writer.Flush();

建立 Syndication 內容

var feed = new SyndicationFeed("標題", "描述", new Uri("https://example.com"));
feed.Items = new[]
{
    new SyndicationItem("項目 1", "內容 1", new Uri("https://example.com/1")),
    new SyndicationItem("項目 2", "內容 2", new Uri("https://example.com/2"))
};

// 輸出 ATOM 1.0
var atomFormatter = new Atom10FeedFormatter(feed);
// 輸出 RSS 2.0
var rssFormatter = new Rss20FeedFormatter(feed);

結合 SOAP 與 Web 端點

同一服務可同時暴露:

  • SOAP 端點:供企業內部/跨企業通訊
  • Web 端點:供外部 Web 客戶端存取

只需在設定檔或程式碼中為同一服務型別加入兩種端點設定。

總結

WCF 3.5 的 HTTP 程式模型增強,填補了 SOA 方案中「服務與 Web 客戶端通訊」的關鍵缺口。WebHttpBinding、WebGet/WebInvoke、WebServiceHostFactory 三劍客讓 REST 服務開發極度簡潔;Syndication 命名空間讓 RSS/ATOM 內容建立與消費不再繁瑣。這是微軟 SOA 戰略邁向開放 Web 的重要一步。

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