WCF 很好的支持了 REST 的开发, 而 RESTful 的服务通常是架构层面上的考虑。 因为它天生就具有很好的跨平台跨语言的集成能力,几乎所有的语言和网络平台都支持 HTTP 请求,无需去实现复杂的客户端代理,无需使用复杂的数据通讯方式既可以将我们的服务暴露给任何需要的人,无论他使用 VB、Ruby、JavaScript,甚至是 HTML FORM,或者直接在浏览器地址栏输入。 
WCF 中通过 WebGetAttribute、WebInvokeAttribute (GET/PUT/POST/DELETE)、UriTemplate 定义 REST 的服务的调用方式, 通过WebMessageFormat (Xml/Json) 定义消息传递的格式。

1. 契约

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

2. 实现

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

通过 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  就能得到结果:

  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也很容易:

  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如何调用这个服务:

  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

WCF4.0 –- RESTful WCF Services (1) (入门)的更多相关文章

  1. WCF4.0 –- RESTful WCF Services

    转自:http://blog.csdn.net/fangxinggood/article/details/6235662 WCF 很好的支持了 REST 的开发, 而 RESTful 的服务通常是架构 ...

  2. CRUD using Spring MVC 4.0 RESTful Web Services and AngularJS

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  3. IIS8.0 部署WCF Services

    今天在Win 8的IIS上部署WCF Services,访问SVC文件时出现找不到处理程序的错误,以前遇到这个问题时都是尝试通过注册asp.net的方式处理一下,但是在Win8下这招不灵了,出现如下提 ...

  4. WCF学习之旅—WCF4.0中的简化配置功能(十五)

    六 WCF4.0中的简化配置功能 WCF4.0为了简化服务配置,提供了默认的终结点.绑定和服务行为.也就是说,在开发WCF服务程序的时候,即使我们不提供显示的 服务终结点,WCF框架也能为我们的服务提 ...

  5. .NET RESTful Web Services入门

    很早之前看到过RESTful Web Services,并未在意,也没找相关资料进行学习.今天偶尔有一机会,就找了点资料进行研究,发现RESTful真是“简约而不简单”.下面用示例来说明: 1 项目结 ...

  6. 如何创建一个RESTful WCF Service

    原创地址:http://www.cnblogs.com/jfzhu/p/4044813.html 转载请注明出处 (一)web.config文件 要创建REST WCF Service,endpoin ...

  7. WCF4.0安装 NET.TCP启用及常见问题

    WCF4.0安装及NET.TCP启用 WCF 4.0 一般默认安装.net Framework 4.0的时候已经安装. 但如果先装.net framework 4.0,后装IIS,就会出现问题.需要重 ...

  8. Jersey the RESTful Web Services in Java

    Jersey 是一个JAX-RS的实现, JAX-RS即Java API for RESTful Web Services, 支持按照表述性状态转移(REST)架构风格创建Web服务. REST 中最 ...

  9. 使用 Spring 3 来创建 RESTful Web Services

    来源于:https://www.ibm.com/developerworks/cn/web/wa-spring3webserv/ 在 Java™ 中,您可以使用以下几种方法来创建 RESTful We ...

随机推荐

  1. AccessRandomFile多线程下载文件

    写一个工具类 package com.pb.thread.demo; import java.io.File; import java.io.FileNotFoundException; import ...

  2. iOS 工厂方法模式

    iOS工厂方法模式 什么是工厂方法模式? 工厂方法模式和简单工厂模式十分类似,大致结构是基本类似的.不同在于工厂方法模式对工厂类进行了进一步的抽象,将之前的一个工厂类抽象成了抽象工厂和工厂子类,抽象工 ...

  3. 很好的UI动效设计参考

    toolBar下拉:

  4. 拿什么守护你的Node.JS进程: Node出错崩溃了怎么办? foreverjs, 文摘随笔

    守护进程 方案一 npm install forever https://github.com/foreverjs/forever 方案二 npm install -g supervisor http ...

  5. Python yield 使用浅析(iterable generator )

    http://blog.csdn.net/preterhuman_peak/article/details/40615201 如何生成斐波那契數列 斐波那契(Fibonacci)數列是一个非常简单的递 ...

  6. Effective Java 37 Use marker interfaces to define types

    Marker interface is an interface that contains no method declarations, but merely designates (or &qu ...

  7. SQLServer中登录名的用户名配置

    其实这个问题困扰我很久了. 今夏(13.7)实习的时候第一次接触sqlserver 当时是统一安排,按部就班的做就行. 那时候链接数据库用的id是sa. 后来自己做小程序时候举得不管什么都用sa登录好 ...

  8. nginx入门(安装,启动,关闭,信号量控制)

    公司使用到了nginx,于是周末初步接触了一下nginx,立即被其简洁,优雅,高效的特性给迷住了.nginx是在是个好东西,配置极其简单,容易理解,极其高效,稍微一调优,ab测试10k并发,很轻松.比 ...

  9. wampserver安装之后连接phpMyAdmin 不成功的解决方法

    情况:我原先安装了本地的mysql数据库,默认密码不是为空,而是123456,但是wampserver安装默认mysql的密码是为空的.所以需要修改一下默认的配置.不然会出现连不上数据库. 解决方案: ...

  10. 移动语义 && 函数调用过程中的 lvalue

    当以一个函数内的临时变量对象作为另一个函数的形参的时候,原函数内的临时对象即 rvalue,就会成为此函数内的 lvalue. 这样会重新导致效率低下,因为造成了大量复制操作. <utility ...