C#实现Rest风格的web服务

C# 实现Rest 风格的web服务

REST(Representational State Transfer)是 RoyFielding 提出的一个描述互联系统架构风格的名词。为什么称为REST?Web本质上由各种各样的资源组成,资源由URI 唯一标识。浏览器(或者任何其它类似于浏览器的应用程序)将展示出该资源的一种表现方式,或者一种表现状态。如果用户在该页面中定向到指向其它资源的链接,则将访问该资源,并表现出它的状态。这意味着客户端应用程序随着每个资源表现状态的不同而发生状态转移,也即所谓REST。

典型的基于 SOAP 的 Web 服务以操作为中心,每个操作接受XML 文档作为输入,提供XML 文档作为输出。在本质上讲,它们是RPC 风格的。而在遵循REST 原则的ROA 应用中,服务是以资源为中心的,对每个资源的操作都是标准化的HTTP 方法。

闲话少说,我们来看看如何用C#实现一个Rest 风格的web服务:

·        定义service 的契约

·        定义URL Routing

·        实现 service

·        为服务编写宿主程序

定义service 的契约

使用VS2010创建一个新的类库工程,命名为RESTServiceLib,为该工程添加引用System.ServiceModel 和System.ServiceModel.Web。创建一个CS文命名为IRESTDemoServices.CS,并定义如下接口:

  1.  
    public interface IRESTDemoServices
  2.  
    {
  3.  
    stringGetClientNameById(string Id);
  4.  
    }

为了让.NetFrameWork识别这是一个service 接口,我们给接口加上ServiceContract特性,给方法加上OperationContract特性:

  1.  
    [ServiceContract(Name = "RESTDemoServices")]
  2.  
    public interface IRESTDemoServices
  3.  
    {
  4.  
    [OperationContract]
  5.  
    string GetClientNameById(int Id);
  6.  
    }

当然我们可以在这个service接口中定义更多的方法,这里是举个简单的例子,就不加其他方法了。

定义URL Routing

新建一个Routing类

  1.  
    public static class Routing
  2.  
    {
  3.  
    public const string GetClientRoute = "/Client/{id}";
  4.  
    }

将Routing与service接口中的方法关联上,注意Routing与service中的参数名称id必须一致(不区分大小写)

  1.  
    [ServiceContract(Name = "RESTDemoServices")]
  2.  
    public interface IRESTDemoServices
  3.  
    {
  4.  
    [OperationContract]
  5.  
    [WebGet(UriTemplate = Routing.GetClientRoute, BodyStyle = WebMessageBodyStyle.Bare)]
  6.  
    string GetClientNameById(string Id);
  7.  
    }

WebGet特性指定GetClientNameById可以被特定的Url以GET方法访问。

实现Service

  1.  
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
  2.  
    ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
  3.  
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  4.  
    public class RestDemoServices:IRESTDemoServices
  5.  
    {
  6.  
    public string GetClientNameById(string Id)
  7.  
    {
  8.  
    Random r = new Random();
  9.  
    string ReturnString="";
  10.  
    int Idnum=Convert.ToInt32(id);
  11.  
    for (int i = 0; i < Idnum; i++)
  12.  
    ReturnString += char.ConvertFromUtf32(r.Next(65, 85));
  13.  
     
  14.  
    return ReturnString;
  15.  
     
  16.  
    }
  17.  
    }

这个方法(GetClientNameById)的实现仅仅是根据id 返回一个长度为id的随机字符串,在实际的项目中更可能是访问数据库得到想要的信息然后返回。

为服务编写宿主程序

使用VS2010创建一个新的Console Application 项目,在项目的属性中将TargetFramework该为.NET Framework 4, 默认是.NET Framework4 client profile,因为.NET Framework4 client profile一般不支持服务器端的辅助类。为项目添加引用System.ServiceModel.Web,并将RESTServiceLib工程添加为引用,其代码实现如下:

  1.  
    static void Main(string[] args)
  2.  
    {
  3.  
    RestDemoServices DemoServices = new RestDemoServices();
  4.  
    WebServiceHost _serviceHost = new WebServiceHost(DemoServices,
  5.  
    new Uri("http://localhost:8000/DEMOService"));
  6.  
    _serviceHost.Open();
  7.  
    Console.ReadKey();
  8.  
    _serviceHost.Close();
  9.  
    }

以上代码为RestDemoServices提供宿主环境,并将其地址指定为http://localhost:8000/DEMOService ,运行该程序,打开IE输入http://localhost:8000/DEMOService/Client/22

若需要使用IIS作为宿主则需要新建一个svc文件,内容如下: 

  1.  
    <%@ ServiceHost Language="C#" Debug="true" Service="RESTService.Lib.RestDemoServices"
  2.  
     
  3.  
    Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>

 

posted @ 2021-02-21 16:59  goodgoodlearn  阅读(573)  评论(0)    收藏  举报