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

.NET Framework 3.5 WCF HTTP 程式設計模型與 Syndication API 完整指南

     編輯:關於.NET

本文基於 Visual Studio 2008 預發布版本,資訊可能變更。涵蓋 HTTP 訊息基礎、URI 資訊嵌入、WCF 與 HTTP 整合、RSS/ATOM 整合應用。技術棧:Windows Communication Foundation。

HTTP 訊息基礎與 Web 架構原則

HTTP 是 Web 的傳輸機制,採用請求/回應訊息交換模式 (MEP)。核心動詞:GET、POST、PUT、DELETE。

HTTP GET:優化的無負載請求

POST /myservice/PostAlbum HTTP 1.1
HOST: www.cloudsample.net
<albumInfo><albumId>15</albumId></albumInfo>

轉換為 GET:

GET /myservice/GetAlbum/15 HTTP 1.1
HOST: www.cloudsamples.net

URI 即負載,訊息更小、傳輸更快,且利於 Proxy 與瀏覽器快取。GET 代表請求資源,資源通常不變。

其他動詞的語意模糊性

  • DELETE:關聯刪減性狀態變更,但具體動作未定義
  • PUT:關聯增加性狀態變更,具體情況因應用而異

URI 中的資訊表達

應用程式利用 URI 嵌入資訊決定回應資源。範例 URI 模式:

contoso.com/artists/[artist]/[album]
contoso.com/artists/[artist]?album=[album]

HTTP 標頭表達額外資訊

圖 1:HTTP GET 請求與回應標頭範例
1 GET /PictureServices/Feed.svc/picture/Pictures;Bridge.jpg HTTP/1.1
2 Accept: image/gif, image/jpeg, application/xaml+xml, */*
3 Accept-Language: en-us
4 UA-CPU: x86
5 Accept-Encoding: gzip, deflate
6 User-Agent: Mozilla/4.0 ...
7 Host: www.cloudsamples.net
8 Proxy-Connection: Keep-Alive

9 HTTP/1.1 200 OK
10 Content-Type: image/jpeg
11 Server: Microsoft-IIS/7.0
12 X-Powered-By: ASP.NET
13 Date: Sat, 15 Sep 2007 18:57:11 GMT
14 Content-Length: 106333

Accept 標頭指定客戶端可接收格式,Content-Type 描述回應資料格式,雙方協調資料格式的簡單機制。

REST 架構風格三大核心

  1. 善用 URI
  2. HTTP GET 具特殊性 (安全、冪等、可快取)
  3. Content-Type 即資料模型

REST 與 WS-* 非互斥,各有優勢:REST 擅長與簡單客戶端 (瀏覽器) 互動;WS-* 擅長訊息路由至中介層。

WCF HTTP 程式設計模型

UriTemplate 與 UriTemplateMatch:URI 處理基石

新增 System.UriTemplate、System.UriTemplateMatch 類型,簡化 URI 建構與解析:

Uri baseAddress = new Uri("https://localhost:2000");
UriTemplate template = new UriTemplate("{artist}?album={album}");
Uri boundUri = template.BindByPosition(baseAddress, "Northwind", "Overdone");
// 結果: https://localhost:2000/Northwind?album=Overdone

UriTemplateMatch match = template.Match(baseAddress, boundUri);
string bandName = match.BoundVariables["artist"]; // "Northwind"

這兩類型不在 System.ServiceModel 命名空間,可獨立於 WCF 使用。

WebGetAttribute 與 WebInvokeAttribute:將操作對應 HTTP 動詞

[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);
}
  • WebGetAttribute:對應 HTTP GET,支援 RequestFormat/ResponseFormat (Xml/Json)
  • WebInvokeAttribute:對應除 GET 外任意 HTTP 動詞,透過 Method 屬性指定

WebOperationContext:HTTP 標頭互動

public Stream GetPicture(string pictureId)
{
    Stream stream = ...; // 取得圖片串流
    WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
    return stream;
}

繫結與行為:WebHttpBinding 與 WebHttpBehavior

WebHttpBinding 抽象 Web 風格傳輸;WebHttpBehavior 設定調度篩選器,使用 UriTemplate 匹配與 HTTP 動詞取代 SOAP Action。

圖 4:建立支援 Web 風格的接收應用程式
ServiceHost host = new ServiceHost(typeof(IPictureContract), new Uri("https://localhost:5000"));
WebHttpBinding binding = new WebHttpBinding();
ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IPictureContract), binding, "");
ep.Behaviors.Add(new WebHttpBehavior());
host.Open();

簡化裝載:WebServiceHost 與 WebServiceHostFactory

  • WebServiceHost:自動為所有端點添加 WebHttpBehavior,執行驗證
  • WebServiceHostFactory:適用 IIS 裝載,在 .svc 指令中指定 Factory
<%@ ServiceHost Language="C#" Service="PictureService" Factory="System.ServiceModel.Web.WebServiceHostFactory" %>

web.config 無需任何 WCF 相關設定。

新整合 API:RSS 與 ATOM 輕鬆建立

整合內容不限於新聞/部落格,可表達任何資料集合。.NET Framework 3.5 完整支援 RSS 2.0、ATOM 1.0 建立與消費,支援擴充,編程模型避免直接處理格式細節。

核心類型

  • SyndicationFeed:整合來源中性表現,包含 SyndicationItem 集合
  • SyndicationFeedFormatter<T>:轉換為特定格式,衍生 Rss20FeedFormatter、Atom10FeedFormatter

建立 SyndicationFeed 兩種方式

手動建構:

SyndicationFeed feed = new SyndicationFeed();
feed.Title.Text = "The Cybertopian Chronicle";
// 設定其他屬性...

從現有來源載入:

Uri feedUri = new Uri("https://blogs.msdn.com/justinjsmith/atom.xml");
SyndicationFeed feed = SyndicationFeed.Load(feedUri);
Console.WriteLine(feed.Title.Text); // "The Cybertopian Chronicle"

SyndicationItem 豐富成員

超過 35 個成員,涵蓋項目識別碼、更新時間、發布日期、標題、內容等,支援各種擴充 (Microsoft Simple List Extensions、Yahoo Media RSS、GeoRSS 等)。

大量項目高效處理

SyndicationFeed.Items 為 IEnumerable<SyndicationItem>,利用迭代器特性與 LINQ 高效處理大量項目。

格式轉換範例:ATOM 轉 RSS

圖 5:轉換整合來源格式
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();

WCF 整合:自訂 URI 暴露整合來源

圖 6:從自訂 URI 暴露整合來源
[ServiceKnownType(typeof(Atom10FeedFormatter))]
[ServiceKnownType(typeof(Rss20FeedFormatter))]
[ServiceContract]
interface IPictureService
{
    [OperationContract]
    [WebGet(UriTemplate = "Pictures?format={format}")]
    SyndicationFeedFormatter<SyndicationFeed> Feed(string format);
}

class PictureService : IPictureService
{
    public SyndicationFeedFormatter<SyndicationFeed> Feed(string format)
    {
        SyndicationFeed feed = new SyndicationFeed();
        // 加入項目...
        if (format.ToLower() == "rss")
            return new Rss20FeedFormatter(feed);
        return new Atom10FeedFormatter(feed);
    }
}

總結與範例應用

掌握 HTTP 基礎、Web 原則、WCF HTTP 程式設計模型與整合 API,可構建兼容 SOAP/WS-* 企業整合與 Web 風格對外服務的統一平台。範例應用 PictureServices (www.cloudsamples.net/pictureservices) 整合本地檔案、Windows 桌面搜尋、Flickr 圖片來源,提供 RSS/ATOM 訂閱與圖片瀏覽功能。


作者:Justin Smith,Microsoft Web 服務技術專家,專注 WCF 與 BizTalk 服務,著作《Inside Windows Communication Foundation》(Microsoft Press, 2007),部落格:blogs.msdn.com/justinjsmith

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