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 服务就好了,希望可以帮助到你

												

5个步骤创建你的第一个RESTFul 服务的更多相关文章

  1. 基于SpringBoot开发一个Restful服务,实现增删改查功能

    前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...

  2. 一个RESTful服务,用来定位运行在AWS地区(Region)中的中间层服务

    Eureka 一个RESTful服务,用来定位运行在AWS地区(Region)中的中间层服务.由两个组件组成:Eureka服务器和Eureka客户端.Eureka服务器用作服务注册服务器.Eureka ...

  3. 适合新手:从零开发一个IM服务端(基于Netty,有完整源码)

    本文由“yuanrw”分享,博客:juejin.im/user/5cefab8451882510eb758606,收录时内容有改动和修订. 0.引言 站长提示:本文适合IM新手阅读,但最好有一定的网络 ...

  4. 译:3.消费一个RESTful Web Service

    这节课我们根据官网教程学习如何去消费(调用)一个 RESTful Web Service . 原文链接 https://spring.io/guides/gs/consuming-rest/ 本指南将 ...

  5. 用Kotlin写一个基于Spring Boot的RESTful服务

    Spring太复杂了,配置这个东西简直就是浪费生命.尤其在没有什么并发压力,随便搞一个RESTful服务 让整个业务跑起来先的情况下,更是么有必要纠结在一堆的XML配置上.显然这么想的人是很多的,于是 ...

  6. 实现一个 RESTful API 服务器

    RESTful 是目前最为流行的一种互联网软件结构.因为它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网站的采用. 什么是 REST REST(REpresentational Stat ...

  7. 第一章 用three.js创建你的第一个3D场景

    第一章 用three.js创建你的第一个3D场景 到官网下载three.js的源码和示例. 创建HTML框架界面 第一个示例的代码如下: 01-basic-skeleton.html 位于 Learn ...

  8. tomcat创建一个windows服务

    具体步骤如下: 1.把JDK解压到C:\Program Files\Java下,Tomcat解压到D:\tomcat下 2.配置环境变量 JAVA_HOME:C:\Program Files\Java ...

  9. 【翻译】在Mac上使用VSCode创建你的第一个Asp.Net Core应用

    Setting Up Your Development Environment 设置你的开发环境 To setup your development machine download and inst ...

随机推荐

  1. 在windos 环境下安装

    在windows 环境下安装node 和 StrongLoop需要一些几个步骤. 本人使用的安装软件,文章最后的分享. 1,安装Git: 2,安装Node.js: 3,安装npm: 4,安装Stron ...

  2. Linux程序设计中的curses.h编译报错,无法找到curses.h和ncurses.h

    源程序screen.c如下: #include <stdio.h> #include <term.h> #include <curses.h> #include & ...

  3. margin负值-内秀篇

    zccst整理 margin系列之布局篇 margin系列之bug巡演(三) margin系列之bug巡演(二) margin系列之内秀篇(二) margin系列之bug巡演 margin系列之内秀篇 ...

  4. Python collections.defaultdict() 与 dict的使用和区别

    看样子这个文档是难以看懂了.直接看示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import collections s = [('yellow', ...

  5. iOS 参考 网络书籍

    网络图书: Xcode中的Project和Target: http://book.51cto.com/art/201307/402283.htm

  6. Sublime text追踪函数插件

    Sublime Text2/3怎样在Ubuntu中配置CTags插件 | 浏览:1278 | 更新:2014-03-05 10:34 1 2 3 4 5 6 7 分步阅读 本文详解在Ubuntu Li ...

  7. QGis(三)查询矢量图层的要素属性字段值(转载)

    QGis(三)查询矢量图层的要素属性字段值 https://github.com/gwaldron/osgearth/issues/489 当加载一个矢量图层后,如果要查看要素的属性字段值,则需要实现 ...

  8. nginx 特定目录禁止php执行

    LNMP有一个缺点就是目录权限设置上不如Apache,有时候网站程序存在上传漏洞或类似pathinfo的漏洞从而导致被上传了php木马,而给网站和服务器带来比较大危险. 建议将网站目录的PHP权限去掉 ...

  9. ie6和ie7下z-index bug的解决办法

    一.匆匆带过的概念 关于CSS中层级z-index的定义啊什么的不是本文的重点,不会花费过多篇幅详细讲述.这里就简单带过,z-index伴随着层的概念产生的.网页 中,层的概念与photoshop或是 ...

  10. Quartz2D 之 简单使用

    1. 获取Graphics Context CGContextRef ctx = UIGraphicsGetCurrentContext(); 2. 最后的渲染接口 CGContextStrokePa ...