理解 RESTful WebService

RESTful 服务遵循REST(Representational State Transfer)的架构风格,中文翻译为:表现层状态转化浏览器

对于全部的CRUD(Read/Create/Update/Delete),RESTFul架构基于HTTP的简单动做(GET,POST,PUT,And DELETE)来实现。它简单并且轻巧,比基于SOAP消息的WebService简单的多的一种轻量级Web服务,RESTful WebService是没有状态的,发布和调用都很是的轻松容易。服务器

表现层(Representation)架构

"资源"是一种信息实体,它能够有多种外在表现形式。咱们把"资源"具体呈现出来的形式,叫作它的"表现层"(Representation)。ui

好比,文本能够用txt格式表现,也能够用HTML格式、XML格式、JSON格式表现,甚至能够采用二进制格式;spa

URI只表明资源的实体,不表明它的形式。URI应该只表明"资源"的位置。它的具体表现形式,应该在HTTP请求的头信息中用Accept和Content-Type字段指定,这两个字段才是对"表现层"的描述。翻译

状态转化(State Transfer):设计

互联网通讯协议HTTP协议,是一个无状态协议。这意味着,全部的状态都保存在服务器端。所以,若是客户端想要操做服务器,必须经过某种手段,让服务器端发生"状态转化"(State Transfer)。而这种转化是创建在表现层之上的,因此就是"表现层状态转化"。rest

客户端用到的手段,只能是HTTP协议。具体来讲,就是HTTP协议里面,四个表示操做方式的动词:GET、POST、PUT、DELETE。它们分别对应四种基本操做:GET用来获取资源,POST用来新建资源(也能够用于更新资源),PUT用来更新资源,DELETE用来删除资源。ci

 

RESTful架构有一些典型的设计误区:资源

一、URI包含动词:由于"资源"表示一种实体,应该是名词,URI不该该有动词,动词应该放在HTTP协议中;

二、URI中加入版本号:由于不一样的版本,能够理解成同一种资源的不一样表现形式,应该采用同一个URI。版本号能够在HTTP请求头信息的Accept字段中进行区分;

 

例子:

1):建立Class Library Project,项目名为RestService

引用System.ServiceModel; System.ServiceModel.Web;

IRestServiceDemo代码以下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace RestService
{
    [ServiceContract(Name = "RestSercice")]
    public interface IRestServiceDemo
    {
        [OperationContract]
        [WebGet(UriTemplate = Routing.GetClientRouting, BodyStyle = WebMessageBodyStyle.Bare)]
        string GetNameById(string id);

        [OperationContract]
        [WebGet(UriTemplate = Routing.GetClientRouting2, BodyStyle = WebMessageBodyStyle.Bare)]
        string GetAgeById(string id);
    }
    public static class Routing
    {
        public const string GetClientRouting = "/Client/{id}";
        public const string GetClientRouting2 = "/Client/zhj/{id}";
    }
}

RestService实现代码以下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;

namespace RestService
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,ConcurrencyMode = ConcurrencyMode.Single,IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class RestServiceDemo:IRestServiceDemo
    {
        public string GetNameById(string id)
        {
            string returnStr = id + "name is John" ;
            return returnStr;
        }
        public string GetAgeById(string id)
        {
            string returnStr = id + "age is 18 years old";
            return returnStr;
        }
    }
}

2):建立Console Application项目,项目名为HostService

引用System.ServiceModel; System.ServiceModel.Web;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RestService;
using System.ServiceModel.Web;

namespace HostService
{
    class Program
    {
        static void Main(string[] args)
        {
            RestServiceDemo restServiceDemo = new RestServiceDemo();
            WebServiceHost serviceHost = new WebServiceHost(restServiceDemo,new Uri("http://localhost:8000/MyService"));
            serviceHost.Open();
            Console.ReadKey();
            serviceHost.Close();
        }
    }
}

 

3):运行Host程序,在浏览器中输入对应Service的Url

http://localhost:8000/MyService/Client/3   输出:3 name is John;

http://localhost:8000/MyService/Client/zhj/3   输出:3 age is 18 years old;

相关文章
相关标签/搜索