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 plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Runtime.Serialization;  
  4. using System.ServiceModel;  
  5. using System.ServiceModel.Web;  
  6.   
  7. namespace WcfRESTfulSvc1  
  8. {  
  9.     [ServiceContract]  
  10.     public interface ITaskService  
  11.     {  
  12.         [OperationContract]  
  13.         [WebGet(UriTemplate="Tasks/Xml", ResponseFormat=WebMessageFormat.Xml)]  
  14.         List<Task> GetTasksXml();  
  15.   
  16.         [OperationContract]  
  17.         [WebGet(UriTemplate = "Tasks/Json", ResponseFormat = WebMessageFormat.Json)]  
  18.         List<Task> GetTasksJson();  
  19.   
  20.         [OperationContract]  
  21.         [WebInvoke(UriTemplate="Task/{title}", Method="GET", ResponseFormat=WebMessageFormat.Json)]  
  22.         Task GetTasksByTitle(string title);  
  23.     }  
  24.   
  25.   
  26.     [DataContract]  
  27.     public class Task  
  28.     {  
  29.         [DataMember]  
  30.         public string Title { get; set; }  
  31.         [DataMember]  
  32.         public string Detail { get; set; }  
  33.         [DataMember]  
  34.         public DateTime CreatedDate { get; set; }  
  35.     }  
  36. }  

 
2. 实现

[c-sharp]  view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4.   
  5. namespace WcfRESTfulSvc1  
  6. {  
  7.     public class TaskService : ITaskService  
  8.     {  
  9.         public List<Task> GetTasksXml()  
  10.         {  
  11.             return GetData();  
  12.         }  
  13.   
  14.         public List<Task> GetTasksJson()  
  15.         {  
  16.             return GetData();  
  17.         }  
  18.   
  19.         public Task GetTasksByTitle(string title)  
  20.         {  
  21.             return GetData().Where(t => t.Title == title).FirstOrDefault();  
  22.         }  
  23.   
  24.         private static List<Task> GetData()  
  25.         {  
  26.             return new List<Task>  
  27.             {  
  28.                 new Task { Title="Task1", Detail="Do Something 1", CreatedDate=DateTime.Now },  
  29.                 new Task { Title="Task2", Detail="Do Something 2", CreatedDate=DateTime.Now },  
  30.                 new Task { Title="Task3", Detail="Do Something 3", CreatedDate=DateTime.Now },  
  31.                 new Task { Title="Task4", Detail="Do Something 4", CreatedDate=DateTime.Now },  
  32.                 new Task { Title="Task5", Detail="Do Something 5", CreatedDate=DateTime.Now },  
  33.             };  
  34.         }  
  35.     }  
  36. }  


通过 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 plain copy
 
  1. <ArrayOfTask xmlns="http://schemas.datacontract.org/2004/07/WcfRESTfulSvc1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">  
  2.   <Task>  
  3.     <CreatedDate>2011-03-09T21:51:13.3376004+08:00</CreatedDate>  
  4.     <Detail>Do Something 1</Detail>  
  5.     <Title>Task1</Title>  
  6.   </Task>  
  7.   <Task>  
  8.     <CreatedDate>2011-03-09T21:51:13.3376004+08:00</CreatedDate>  
  9.     <Detail>Do Something 2</Detail>  
  10.     <Title>Task2</Title>  
  11.   </Task>  
  12.   <Task>  
  13.     <CreatedDate>2011-03-09T21:51:13.3376004+08:00</CreatedDate>  
  14.     <Detail>Do Something 3</Detail>  
  15.     <Title>Task3</Title>  
  16.   </Task>  
  17.   <Task>  
  18.     <CreatedDate>2011-03-09T21:51:13.3376004+08:00</CreatedDate>  
  19.     <Detail>Do Something 4</Detail>  
  20.     <Title>Task4</Title>  
  21.   </Task>  
  22.   <Task>  
  23.     <CreatedDate>2011-03-09T21:51:13.3376004+08:00</CreatedDate>  
  24.     <Detail>Do Something 5</Detail>  
  25.     <Title>Task5</Title>  
  26.   </Task>  
  27. </ArrayOfTask>  



客户端的调用利用System.Net.WebClient也很容易:

[c-sharp]  view plain copy
 
  1. var client = new WebClient();  
  2. 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 plain copy
 
  1. <mce:script type="text/javascript" language="JavaScript"><!--  
  2.     $(document).ready(function () {  
  3.         $("#btnGet").click(function () {  
  4.             var url = $("#txtUrl").val();  
  5.             $.get(url, function (data) {  
  6.                 for (var i = 0; i < data.length; i++)  
  7.                     $("#divResponse").append("<li>" +  
  8.                     data[i].Title + "&nbsp;-&nbsp;" +  
  9.                     data[i].Detail + "</li>");  
  10.             });  
  11.         });  
  12.     });  
  13. // --></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

 

分类:  ASP.NET, WCF
本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/p/3796656.html ,如需转载请自行联系原作者

相关文章
|
7月前
|
Oracle 关系型数据库 API
C# LIS检验系统源码,接口技术:RESTful API + Http+WCF
LIS检验系统一种专门用于医院化验室的计算机系统,它致力于提高医院化验室的工作效率和检测准确率。LIS系统由多个子系统组成,包括样本管理系统、质控系统、检验结果管理系统、报告管理系统等。体系结构:Client/Server架构 SaaS模式 客户端:WPF+Windows Forms 服务端:C# +.Net 数据库:Oracle 接口技术:RESTful API + Http+WCF
|
API 网络架构
WCF的一点补充-Restful相关
参考 配置WCF心得 对REST架构 风格下WCF的一点补充 Securing WCF REST Service with Azure AppFabric Access Control Service and OAuth(WRAP) RESTful API 设计指南 使用webclient...
768 0
|
XML JSON C#
WCF 添加 RESTful 支持,适用于 IIS、Winform、cmd 宿主
You can expose the service in two different endpoints. the SOAP one can use the binding that support SOAP e.
1536 0
|
XML JSON 数据格式
如何创建一个RESTful WCF Service
原创地址:http://www.cnblogs.com/jfzhu/p/4044813.html 转载请注明出处   (一)web.config文件 要创建REST WCF Service,endpoint binding需要用webHttpBinding,参见《webHttpBinding、basicHttpBinding和wsHttpBinding的区别》。
1075 0