使用asp.net和wCF4.0开发RESTful服务
来源:互联网 发布:手机上网代理软件 编辑:程序博客网 时间:2024/06/04 19:48
一.什么是wcf?
百度百科的定义:
Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台。
整合了原有的windows通讯的 .net Remoting,WebService,Socket的机制,并融合有HTTP和FTP的相关技术。
是Windows平台上开发分布式应用最佳的实践方式。
简单的归结为四大部分
1>.网络服务的协议,即用什么网络协议开放客户端接入。
2>.业务服务的协议,即声明服务提供哪些业务。
3>.数据类型声明,即对客户端与服务器端通信的数据部分进行一致化。
4>.传输安全性相关的定义。它是.net框架的一部分,由 .NET Framework3.0 开始引入,与Windows Presentation Foundation及Windows Workflow Foundation并行为新一代 Windows操作系统以及 WinFX 的三个重大应用程序开发类库。在.net framework 2.0 以及前版本中,微软发展了 web service (SOAP with HTTP communication),.NET Remoting (TCP/HTTP/Pipeline communication) 以及基础的 Winsock 等通信支持。由于各个通信方法的设计方法不同,而且彼此之间也有相互的重叠性,对于开发人员来说,不同的选择会有不同的程序设计模型,而且必须要重新学习,让开发人员在使用中有许多不便。同时,面向服务架构(Service-Oriented Architecture) 也开始盛行于软件工业中,因此微软重新查看了这些通信方法,并设计了一个统一的程序开发模型,对于数据通信提供了最基本最有弹性的支持,这就是 Windows Communication Foundation。
优势:统一性、互操作性、安全与可依赖性、兼容性。
more:http://baike.baidu.com/link?url=f8V5k-bQDjbF8oyybimKOsYl24NqFUrZ1lD70LNoG9WydZygBPh5uvdn4pHSTP9FyRW1ahcQPniWHyzMRZF9RK
二.什么是RESTful?
1.一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
2.RESTful的关键是定义可表示流程元素/资源的对象。在REST中,每一个对象都是通过URL来表示的,对象用户负责将状态信息打包进每一条消息内,以便对象的处理总是无状态的。
REST使用一些常见的HTTP方法来插入、删除、更新、返回信息,这些方法如下:
①GET - 请求针对资源的特定表达式
②PUT - 根据提供的表达式创建或者更新一个资源
③DELETE - 删除指定的资源
④POST - 提交数据来让指定的资源处理
3.为何使用?
① 更少的开销(对于每次调用不需要包裹SOAP外壳)②更少的重复(HTTP已经提供了诸如DELETE、PUT、GET等等的方法,否则将表现在一个SOAP外壳当中)。③更标准化 - HTTP方法很容易理解并且操作一致。一些SOAP实现可能变得过分挑剔。④对于人类有更强的可读性和可测试性(很难仅仅用一个浏览器来测试SOAP)。⑤不需要使用XML(好吧,对于SOAP貌似也不需要使用,不过这很难成立,因为你已经开始解析外壳了)⑥库使得SOAP(有点)容易。但正如我提到的,你正在抽出大量的底层冗余。确实,在理论上,在避免装载功能相同的顶层这一项上,SOAP能够超过其他的转换;但是在现实中你可能做的几乎所有SOAP工作,都是通过HTTP的。
more:http://www.csdn.net/article/2013-06-13/2815744-RESTful-API
三.使用asp.net和wCF4.0开发RESTful服务
第一步:1.新建一个类项目
2.建取三个类
一个IBookService.cs接口,代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.ServiceModel;using System.ServiceModel.Web;using System.Text;using System.Threading.Tasks;using WCFClass.Model;namespace WCFClass{ [ServiceContract] public interface IBookService { [OperationContract] [WebGet(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/GetBookPrice/For/BookCode/{bookCode}")] Book GetPriceByBookCode(string bookCode); [OperationContract] [WebGet (RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/GetBookPrice/For/City/{city}")] Book GetPriceByCity(string city); }}
WebGet属性
WebGet属性让我们能够定制在处理REST服务的过程中,如何处理HTTP的GET方法。如例子所示,我们在URL中呈现GET方法,并且我们期待返回一些POX(简单的旧式XML)结果。
- 请求格式:是发起请求的格式。两个选项是XML和JSON。如果你从浏览器请求,你的请求将会被重视。
- 响应格式:是返回的格式。两个选项是XML和JSON。在这里我们定制我们想要通过XML返回。
- 数据体样式:展现了我们希望怎样返回数据。
UriTemplate描述了我们将如何处理 Uri映射。这个例子中,我们声明了基地址(BaseAddress)之后的URL看起来是下面这样的:
/GetBookPrice/For/BookCode/{bookCode}
这就是服务合同,为我们REST服务实现了两个方法。一个通过城市名字得到书的价格,另一个通过书编号获得。
BookService.cs代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.ServiceModel.Activation;using System.Text;using System.Threading.Tasks;namespace WCFClass{ [AspNetCompatibilityRequirements( RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class BookService:IBookService { public Model.Book GetPriceByBookCode(string bookCode) { switch (bookCode) { case "00000": return new Model.Book() { Name = "None", Price = 20.12 }; case "12345": return new Model.Book() { Name = "WCF Learning", Price = 40.12 }; case "11111": return new Model.Book() { Name = "RestFul Guide", Price = 30.12 }; default: return new Model.Book() { Name = "ES", Price = 50.12 }; } } public Model.Book GetPriceByCity(string city) { switch (city) { case "shanghai": return new Model.Book() { Name = "None", Price = 20.12 }; case "beijing": return new Model.Book() { Name = "WCF Learning", Price = 40.12 }; case "wuhan": return new Model.Book() { Name = "RestFul Guide", Price = 30.12 }; default: return new Model.Book() { Name = "ES", Price = 50.12 }; } } }}Model.Book.cs代码如下
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.Text;using System.Threading.Tasks;namespace WCFClass.Model{ [DataContract] public class Book { [DataMember] public string Name { get; set; } [DataMember] public string Code { get; set; } [DataMember] public double Price { get; set; } }}第二步:1.创建一个web应用程序
2.建立全局Global.asax 代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.ServiceModel;using System.ServiceModel.Web;using System.Text;using System.Threading.Tasks;using WCFClass.Model;namespace WCFClass{ [ServiceContract] public interface IBookService { [OperationContract] [WebGet(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/GetBookPrice/For/BookCode/{bookCode}")] Book GetPriceByBookCode(string bookCode); [OperationContract] [WebGet (RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/GetBookPrice/For/City/{city}")] Book GetPriceByCity(string city); }}3.web.config中 添加代码:
<system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel>最终运行:
参照:http://www.oschina.net/translate/developing-restful-service-with-wcf-4-0-using-asp
0 0
- 使用asp.net和wCF4.0开发RESTful服务
- 使用 ASP.NET 和 WCF 4.0 开发 RESTful 服务
- [ASP.net教程]WCF4.0进阶系列--第七章 维持会话状态和设置服务操作的顺序
- WCF4.0 -- RESTful WCF Services(创建基于SSL的服务)
- WCF4.0 -- RESTful WCF Services(创建基于SSL的服务)
- WCF4.0 -- RESTful WCF Services(创建基于SSL的服务)
- WCF4.0 –- RESTful WCF Services (实例) (并发同步服务 SyncService)
- WCF4.0 –- RESTful WCF Services (实例) (并发同步服务 SyncService)
- WCF4.0 –- RESTful WCF Services (实例) (并发同步服务 SyncService)
- WCF4.0 –- RESTful WCF Services
- WCF4.0 –- RESTful WCF Services
- 使用CXF开发RESTFul服务
- 使用CXF开发RESTFul服务
- 使用CXF开发RESTFul服务
- 使用CXF开发RESTFul服务
- 使用Jersey开发RESTful服务
- 使用CXF开发RESTFul服务
- 使用Jersey开发RESTful服务
- sql 复制服务器文件
- pjsua帮助手册(中文)
- 在Ubuntu下编译FFmpeg
- [BZOJ1516] [POI2006]Mag-Warehouse
- Volley 的介绍和使用(三)
- 使用asp.net和wCF4.0开发RESTful服务
- 拓扑结构相同子树练习题
- 李克强总理为啥要在政府工作报告中提倡工匠精神?
- java IO流读写文件
- 15_Android性能优化
- java socket 通信编程
- js中巧用cssText属性批量操作样式(转)
- Android中Intent.action的各种常见作用
- How to change tintColor of UIBarButtonItem? [SWIFT]