日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WCF4.0 –- RESTful WCF Services (1) (入门)

發布時間:2024/9/20 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WCF4.0 –- RESTful WCF Services (1) (入门) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

WCF 很好的支持了 REST 的開發, 而 RESTful 的服務通常是架構層面上的考慮。 因為它天生就具有很好的跨平臺跨語言的集成能力,幾乎所有的語言和網絡平臺都支持 HTTP 請求,無需去實現復雜的客戶端代理,無需使用復雜的數據通訊方式既可以將我們的服務暴露給任何需要的人,無論他使用 VB、Ruby、JavaScript,甚至是 HTML FORM,或者直接在瀏覽器地址欄輸入。?
WCF 中通過?WebGetAttribute、WebInvokeAttribute (GET/PUT/POST/DELETE)、UriTemplate?定義 REST 的服務的調用方式, 通過WebMessageFormat (Xml/Json)?定義消息傳遞的格式。1. 契約

[c-sharp]?view plaincopy
  • using?System;??
  • using?System.Collections.Generic;??
  • using?System.Runtime.Serialization;??
  • using?System.ServiceModel;??
  • using?System.ServiceModel.Web;??
  • ??
  • namespace?WcfRESTfulSvc1??
  • {??
  • ????[ServiceContract]??
  • ????public?interface?ITaskService??
  • ????{??
  • ????????[OperationContract]??
  • ????????[WebGet(UriTemplate="Tasks/Xml",?ResponseFormat=WebMessageFormat.Xml)]??
  • ????????List<Task>?GetTasksXml();??
  • ??
  • ????????[OperationContract]??
  • ????????[WebGet(UriTemplate?=?"Tasks/Json",?ResponseFormat?=?WebMessageFormat.Json)]??
  • ????????List<Task>?GetTasksJson();??
  • ??
  • ????????[OperationContract]??
  • ????????[WebInvoke(UriTemplate="Task/{title}",?Method="GET",?ResponseFormat=WebMessageFormat.Json)]??
  • ????????Task?GetTasksByTitle(string?title);??
  • ????}??
  • ??
  • ??
  • ????[DataContract]??
  • ????public?class?Task??
  • ????{??
  • ????????[DataMember]??
  • ????????public?string?Title?{?get;?set;?}??
  • ????????[DataMember]??
  • ????????public?string?Detail?{?get;?set;?}??
  • ????????[DataMember]??
  • ????????public?DateTime?CreatedDate?{?get;?set;?}??
  • ????}??
  • }??
  • ?2. 實現

    [c-sharp]?view plaincopy
  • using?System;??
  • using?System.Collections.Generic;??
  • using?System.Linq;??
  • ??
  • namespace?WcfRESTfulSvc1??
  • {??
  • ????public?class?TaskService?:?ITaskService??
  • ????{??
  • ????????public?List<Task>?GetTasksXml()??
  • ????????{??
  • ????????????return?GetData();??
  • ????????}??
  • ??
  • ????????public?List<Task>?GetTasksJson()??
  • ????????{??
  • ????????????return?GetData();??
  • ????????}??
  • ??
  • ????????public?Task?GetTasksByTitle(string?title)??
  • ????????{??
  • ????????????return?GetData().Where(t?=>?t.Title?==?title).FirstOrDefault();??
  • ????????}??
  • ??
  • ????????private?static?List<Task>?GetData()??
  • ????????{??
  • ????????????return?new?List<Task>??
  • ????????????{??
  • ????????????????new?Task?{?Title="Task1",?Detail="Do?Something?1",?CreatedDate=DateTime.Now?},??
  • ????????????????new?Task?{?Title="Task2",?Detail="Do?Something?2",?CreatedDate=DateTime.Now?},??
  • ????????????????new?Task?{?Title="Task3",?Detail="Do?Something?3",?CreatedDate=DateTime.Now?},??
  • ????????????????new?Task?{?Title="Task4",?Detail="Do?Something?4",?CreatedDate=DateTime.Now?},??
  • ????????????????new?Task?{?Title="Task5",?Detail="Do?Something?5",?CreatedDate=DateTime.Now?},??
  • ????????????};??
  • ????????}??
  • ????}??
  • }??

  • 通過 WCF 4.0 里創建的 WCF Service Application 發布REST服務很簡單,只需要在 svc 的 Markup 里加上 Factory:
    <%@ ServiceHost Language="C#" Debug="true" Service="WcfRESTfulSvc1.TaskService" CodeBehind="TaskService.svc.cs"Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>
    BTW: 不過這樣,WCF的Metadata就不能訪問到了,也就說不能訪問到svc的wsdl了。
    OK,在瀏覽器中鍵入?http://localhost:2571/TaskService.svc/Tasks/Xml? 就能得到結果:

    [xhtml]?view plaincopy
  • <ArrayOfTask?xmlns="http://schemas.datacontract.org/2004/07/WcfRESTfulSvc1"?xmlns:i="http://www.w3.org/2001/XMLSchema-instance">??
  • ??<Task>??
  • ????<CreatedDate>2011-03-09T21:51:13.3376004+08:00</CreatedDate>??
  • ????<Detail>Do?Something?1</Detail>??
  • ????<Title>Task1</Title>??
  • ??</Task>??
  • ??<Task>??
  • ????<CreatedDate>2011-03-09T21:51:13.3376004+08:00</CreatedDate>??
  • ????<Detail>Do?Something?2</Detail>??
  • ????<Title>Task2</Title>??
  • ??</Task>??
  • ??<Task>??
  • ????<CreatedDate>2011-03-09T21:51:13.3376004+08:00</CreatedDate>??
  • ????<Detail>Do?Something?3</Detail>??
  • ????<Title>Task3</Title>??
  • ??</Task>??
  • ??<Task>??
  • ????<CreatedDate>2011-03-09T21:51:13.3376004+08:00</CreatedDate>??
  • ????<Detail>Do?Something?4</Detail>??
  • ????<Title>Task4</Title>??
  • ??</Task>??
  • ??<Task>??
  • ????<CreatedDate>2011-03-09T21:51:13.3376004+08:00</CreatedDate>??
  • ????<Detail>Do?Something?5</Detail>??
  • ????<Title>Task5</Title>??
  • ??</Task>??
  • </ArrayOfTask>??

  • 客戶端的調用利用System.Net.WebClient也很容易:

    [c-sharp]?view plaincopy
  • var?client?=?new?WebClient();??
  • this.txtResponse.Text?=?client.DownloadString(url);??

  • Json的返回結果:
    [{"CreatedDate":"//Date(1299687080328+0800)//","Detail":"Do Something 1","Title":"Task1"},{"CreatedDate":"//Date(1299687080328+0800)//","Detail":"Do Something 2","Title":"Task2"},{"CreatedDate":"//Date(1299687080328+0800)//","Detail":"Do Something 3","Title":"Task3"},{"CreatedDate":"//Date(1299687080328+0800)//","Detail":"Do Something 4","Title":"Task4"},{"CreatedDate":"//Date(1299687080328+0800)//","Detail":"Do Something 5","Title":"Task5"}]
    再來看看利用jQuery如何調用這個服務:

    [javascript]?view plaincopy
  • <mce:script?type="text/javascript"?language="JavaScript"><!--??
  • ????$(document).ready(function?()?{??
  • ????????$("#btnGet").click(function?()?{??
  • ????????????var?url?=?$("#txtUrl").val();??
  • ????????????$.get(url,?function?(data)?{??
  • ????????????????for?(var?i?=?0;?i?<?data.length;?i++)??
  • ????????????????????$("#divResponse").append("<li>"?+??
  • ????????????????????data[i].Title?+?"&nbsp;-&nbsp;"?+??
  • ????????????????????data[i].Detail?+?"</li>");??
  • ????????????});??
  • ????????});??
  • ????});??
  • //?--></mce:script>??

  • 【REST WCF系列】RESTful WCF Services (1) (入門)RESTful WCF Services (2) (實現增,刪,改,查)RESTful WCF Services (3) (Raw Stream)RESTful WCF Services (4) (Basic Security)RESTful WCF Services (實例) (并發同步服務 SyncService)

    http://blog.csdn.net/fangxing80/article/details/6235662

    總結

    以上是生活随笔為你收集整理的WCF4.0 –- RESTful WCF Services (1) (入门)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。