使用asp.net和wCF4.0开发RESTful服务

一.什么是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。
优势:统一性、互操作性、安全与可依赖性、兼容性。

二.什么是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的。 



三.使用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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值