1、啥是RESTFul 服务

在我们创建简单小程序前,先来学习下RESTFul 服务。RESTFul服务就是遵循了 Representational State Transfer(可以参考http://blog.csdn.net/zhruifei/article/details/50633495) 这个架构的一种架构。WCF允许我们使用SOAP 通过各种协议,协议包括,HTTP,TCP,MSMQ,Named Pipes等进行交换信息。现在我们通过一个最常用的协议HTTP协议来讲述WCF服务,REST服务通过HTTP来进行最常用的CRUD(Read(GET)/Create(POST)/Update(PUT)/Delete(DELETE))功能,在这里我们先实现一个简单的GET功能

2、创建restful 服务

下面是5步创建你的rest服务并且返回xml格式

  • 创建WCF Service Project.
  • 准备数据
  • 创建Service Contract
  • 继承Service
  • 配置服务和行为

1)打开vs-新建项目-选择WCF服务应用程序

2,新建一个类 car.cs

  [DataContract]
public class Car
{
[DataMember]
public string color { get; set; }
[DataMember]
public double speed { get; set; }
[DataMember]
public double price { get; set; }
} public partial class Cars
{
public static readonly Cars _instance = new Cars();
private Cars() { } public static Cars Instance
{
get { return _instance; }
} public List<Car> CarList
{
get { return carLists; }
}
private List<Car> carLists = new List<Car>
{
new Car { color = "red", speed = , price = },
new Car{color = "blue", speed = , price = },
new Car{color="green",speed=,price=}
}; }

3)新建WCF 服务--如下图所示

系统将新疆两个文件,包括ICarRestService.cs 接口文件如下图所示:

下面我们将Dowork 方法改为GetCarList 方法

如下所示:

  • Method="Get" 代表这Http获取数据的方式
  • RequestFormat = WebMessageFormat.Json  请求的数据是JSON格式,当然RequestFormat = WebMessageFormat.Xml 请求的是XML格式
  • UriTemplate = "GetCarList/" 请求了URL

5)配置服务和行为

<?xml version="1.0"?>
<configuration> <appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service name="MyRESTService.ProductRESTService" behaviorConfiguration="serviceBehavior">
<endpoint address=""
binding="webHttpBinding"
contract="MyRESTService.IProductRESTService"
behaviorConfiguration="web"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer> </configuration>

  配置自己写写就明白了  此处的webHTTPBinding 是rest 服务专用的绑定模式

6)然后在浏览器中输入:http://localhost:30547/CarsRestService.svc/GetCarList/

得到的结果如下:

哈哈这样第一个rest 服务就好了,希望可以帮助到你

												

最新文章

  1. 错误: 从内部类中访问本 地变量vvv; 需要被声明为最终类型
  2. [LeetCode] Strobogrammatic Number 对称数
  3. requireJs--简单的使用方法
  4. 一道js面试题看变量的作用域
  5. 分享一本Swift好书
  6. jquery 删除字符串最后一个字符的方法
  7. C# DataTable转换成DataRow
  8. 一次Oracle数据迁移
  9. A Tour of Go For is Go&#39;s &quot;while&quot;
  10. java 小数点取2位并且四舍五入
  11. npm 常用命令
  12. 【转】Android的Merge讲解与实例
  13. jQuery判断当前元素是第几个元素&amp;获取第N个元素
  14. SQL关键字转换大写核心算法实现
  15. 使sublimetext3在ubuntu下可以打中文和在windows的dos命令行下正常显示中文
  16. Struts2之i18N国际化
  17. C# 数组与集合的区别
  18. python语法之函数2
  19. java 返回某一天的周日和现在这一周的周日
  20. MIPI接口LCD屏调试心得(转)

热门文章

  1. UVA 11551 Experienced Endeavour
  2. UDP网络程序模型设计
  3. html小知识
  4. 学习生命周期activity
  5. python 爬取的数据要如何展现(可视化)?
  6. Ubuntu切换默认语言
  7. Visual Studio Team Services 帐户管理操作
  8. imageX批量安裝windows7
  9. (转载)HTML、CSS、JavaScript、PHP、MySQL 的学习顺序是什么?
  10. centos5.5 Apache2 Web 服务器的安装