原创地址:http://www.cnblogs.com/jfzhu/p/4044813.html

转载请注明出处

(一)web.config文件

要创建REST WCF Service,endpoint binding需要用webHttpBinding,参见《webHttpBinding、basicHttpBinding和wsHttpBinding的区别》

web.config

<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="None" />
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="SandwichServices.CostServiceBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="SandwichServices.CostServiceServiceBehavior" >
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="SandwichServices.CostService" behaviorConfiguration="SandwichServices.CostServiceServiceBehavior">
<endpoint address="" behaviorConfiguration="SandwichServices.CostServiceBehavior"
binding="webHttpBinding" contract="SandwichServices.CostService" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services>
</system.serviceModel>
</configuration>

《如何创建一个AJAX-Enabled WCF Service》中的web.config,因为需要AJAX,endpointBehaviors用了<enableWebScript />,但是enableWebScript 和REST需要的UriTemplate是有冲突的,所以这里不再使用。

endpointBehaviors中设置<webHttp helpEnabled="true"/>可以生成WCF Service的Help页面。

HTTP定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE。一个URL地址,它用于描述一个网络上的资源,而HTTP中的GET,POST,PUT,DELETE就对应着对这个资源的查,改,增,删4个操作。GET一般用于获取/查询资源信息,而POST一般用于更新资源信息。

对于PUT和DELETE,需要身份验证信息,所以我们先暂时只允许匿名访问,在web.config中将authentication mode设置为None。

(二)webHttpBinding的XML格式

Employee.cs

using System;
using System.Runtime.Serialization; namespace SandwichServices
{
[DataContract]
public class Employee
{
private Guid id;
private string name;
private DateTime birthdate; [DataMember]
public Guid Id
{
get { return id; }
set { id = value; }
} [DataMember]
public string Name
{
get { return name; }
set { name = value; }
} [DataMember]
public DateTime Birthdate
{
get { return birthdate; }
set { birthdate = value; }
}
}
}

CostService.svc.cs

using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web; namespace SandwichServices
{
[ServiceContract(Namespace = "SandwichServices")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CostService
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// and include the following line in the operation body:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "Employees/AddEmployee", ResponseFormat = WebMessageFormat.Xml)]
public Guid AddEmployee(Employee employee)
{
return Guid.NewGuid();
} [OperationContract]
[WebInvoke(Method = "DELETE", UriTemplate = "Employees/DeleteEmployee?id={id}", ResponseFormat = WebMessageFormat.Xml)]
public void DeleteEmployee(string id)
{
return;
} [OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Employees/UpdateEmployee", ResponseFormat = WebMessageFormat.Xml)]
public void UpdateEmployee(Employee employee)
{
return;
} [OperationContract]
[WebGet(UriTemplate = "Employees/GetEmployee?id={id}", ResponseFormat = WebMessageFormat.Xml)]
public Employee GetEmployee(string id)
{
return new Employee() { Id = new Guid(id), Name = "Neil Klugman", Birthdate = new DateTime(, , ) };
}
}
}

(三)webHttpBinding JSON格式

将每个方法的ResponseFormat改为Json

CostService.svc.cs

using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web; namespace SandwichServices
{
[ServiceContract(Namespace = "SandwichServices")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CostService
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// and include the following line in the operation body:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "Employees/AddEmployee", ResponseFormat=WebMessageFormat.Json)]
public Guid AddEmployee(Employee employee)
{
return Guid.NewGuid();
} [OperationContract]
[WebInvoke(Method = "DELETE", UriTemplate = "Employees/DeleteEmployee?id={id}", ResponseFormat = WebMessageFormat.Json)]
public void DeleteEmployee(string id)
{
return;
} [OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Employees/UpdateEmployee", ResponseFormat = WebMessageFormat.Json)]
public void UpdateEmployee(Employee employee)
{
return;
} [OperationContract]
[WebGet(UriTemplate = "Employees/GetEmployee?id={id}", ResponseFormat = WebMessageFormat.Json)]
public Employee GetEmployee(string id)
{
return new Employee() { Id = new Guid(id), Name = "Neil Klugman", Birthdate = new DateTime(, , ) };
}
}
}

(四)总结

  1. RESTful WCF Service需要使用webHttpBinding
  2. endpointBehaviors不要用<enableWebScript />
  3. endpointBehaviors中设置<webHttp helpEnabled="true"/>可以生成WCF Service的Help页面
  4. GET(查),POST(改),PUT(增),DELETE(删)
  5. 对于PUT和DELETE,需要身份验证信息
  6. webHttpBinding的数据格式有两种:XML和JSON,可以通过ResponseFormat来设置

如何创建一个RESTful WCF Service的更多相关文章

  1. 如何创建一个AJAX-Enabled WCF Service

      原创地址:http://www.cnblogs.com/jfzhu/p/4041638.html 转载请注明出处   前面的文章中介绍过<Step by Step 创建一个WCF Servi ...

  2. 用Spring Tools Suite(STS)开始一个RESTful Web Service

    spring.io官方提供的例子Building a RESTful Web Service提供了用Maven.Gradle.STS构建一个RESTFul Web Service,实际上采用STS构建 ...

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

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

  4. 通过beego快速创建一个Restful风格API项目及API文档自动化

    通过beego快速创建一个Restful风格API项目及API文档自动化 本文演示如何快速(一分钟内,不写一行代码)的根据数据库及表创建一个Restful风格的API项目,及提供便于在线测试API的界 ...

  5. 通过beego快速创建一个Restful风格API项目及API文档自动化(转)

    通过beego快速创建一个Restful风格API项目及API文档自动化 本文演示如何快速(一分钟内,不写一行代码)的根据数据库及表创建一个Restful风格的API项目,及提供便于在线测试API的界 ...

  6. 在GlassFish应用服务器上创建并运行你的第一个Restful Web Service【翻译】

    前言 本人一直开发Android应用,目前Android就业形势恶劣,甚至会一路下滑,因此决定学习服务器开发.采用的语言是java,IDE是Intellij,在下载Intellij的同时看到官网很多优 ...

  7. Spring Boot 构建一个 RESTful Web Service

    1  项目目标: 构建一个 web service,接收get 请求 http://localhost:8080/greeting 响应一个json 结果: {"id":1,&qu ...

  8. ASP.NET MVC提交一个较复杂对象至WCF Service

    前一篇<jQuery.Ajax()执行WCF Service的方法>http://www.cnblogs.com/insus/p/3727875.html 我们有练习在asp.net mv ...

  9. 【转】Spring 4.x实现Restful web service

    http://my.oschina.net/yuyidi/blog/352909 首先我们还是跟之前一样,创建一个maven项目,不过因为Spring Restful web service是基于Sp ...

随机推荐

  1. 被Unity5坑惨了

    各种不明所以的crash,导致crash率从0.5%瞬间暴涨到10%. Unity5还是非常不稳定,慎入慎入...

  2. [算法总结]three-way partition

    procedure three-way-partition(A : array of values, mid : value): i ← 0 j ← 0 n ← size of A - 1 while ...

  3. 大家都在用PDA条码扫描枪管理企业仓库 PDA无线数据采集程序

    PDA数据采集器又称之为手持终端,这些都是用于扫描货物条码统计数据用的,PDA扫描枪有效提高企业仓库管理,在仓库管理中引入条码技术,对仓库的到货检验.入库.出库.调拨.移库移位.库存盘点等各个作业环节 ...

  4. 如何理解typedef void (*pfun)(void)

    问题: 在刚接触typedef void (*pfun)(void) 这个结构的时候,存在疑惑,为什么typedef后只有一"块"东西,而不是两"块"东西呢?那 ...

  5. solr4.5安装配置 linux+tomcat6.0+mmseg4j-1.9.1分词

    首先先介绍下solr的安装配置 solr下载地址 (我这用的solr-4.5.0) 运行环境 JDK 1.5或更高版本 下载地址(Solr 4以上版本,要求JDK 1.6)  我用的JDK1.6 ) ...

  6. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  7. CSharp数据库代码生成工具

    项目中遇到很多数据库表字段特别多的项目,手动一个一个去敲也不知道敲到什么时候,突发奇想做一个工具一劳永逸.花了一晚上做了这个东西,代码写的比较乱,用这个工具后减少了很多时间. Git地址:https: ...

  8. jquery事件核心源码分析

    我们从绑定事件开始,一步步往下看: 以jquery.1.8.3为例,平时通过jquery绑定事件最常用的是on方法,大概分为下面3种类型: $(target).on('click',function( ...

  9. 子代选择器(>)后代选择器(' ')的区别

    子代选择器是指紧接着父级的那个标签,如:container>a指的是紧接着container后面的第一个a(儿子级别的,孙子或者之后的a是不能生效的) 后代选择器是用空格分开的,如:contai ...

  10. 懒加载lazyload

    什么是懒加载 懒加载就是当你做滚动到页面某个位置,然后再显示当前位置的图片,这样做可以减少页面请求. 懒加载:主要目的是作为服务器前端的优化,减少请求数或延迟请求数,一些图片非常多的网站中非常有用,在 ...