zoukankan      html  css  js  c++  java
  • WCF 定义SOAP和REST风格的webservice

    摘抄于其他帖子,在此记录以备后用。

    1. 定义服务数据契约(SOAP与REST方式相同)

     public class Employee
       {

           [DataMember]
           public string Id { get; set; }
        

           [DataMember]
           public string Name { get; set; }
        

          [DataMember]
          public string Department { get; set; }
       

          [DataMember]
          public string Grade { get; set; }
       

          public override string ToString()
          {
              return string.Format("ID: {0,-5}姓名: {1, -5}级别: {2, -4} 部门: {3}",Id, Name, Grade, Department);
          }
      }
     

    2. 定义服务行为契约(SOAP与REST方式相同)

    接下来我们定义了如下一个表示服务契约的接口IEmployeesService。和基于SOAP的服务契约定义不同,我们无需在相应的操作方法上 面应用OperationContractAttribute特性,但是应用在接口/类上的ServiceContractAttribute特性仍是必需的。在这里替换OperationContractAttribute特性的分别是WebGetAttributeWebInvokeAttribute,它们均定义在System.ServiceModel.Web程序集中。

    --REST

     [ServiceContract]   
    public interface IEmployees
       {
           [WebGet(UriTemplate = "all")]
           IEnumerable<Employee> GetAll();
        

           [WebGet(UriTemplate = "{id}")]
           Employee Get(string id);
        

           [WebInvoke(UriTemplate = "/", Method = "POST")]
           void Create(Employee employee);
        

           [WebInvoke(UriTemplate = "/", Method = "PUT")]
           void Update(Employee employee);
        

           [WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
           void Delete(string id);

       }



    --SOAP

      [ServiceContract]

      public interface IEmployees
       {
           [OperationContract]
           IEnumerable<Employee> GetAll();
        

           [OperationContract]
           Employee Get(string id);
        

           [OperationContract]
           void Create(Employee employee);
        

           [OperationContract]
           void Update(Employee employee);
        

           [OperationContract]
           void Delete(string id);

       }

    契约接口IEmployeesService中定义了5个操作,分别用于实现针对员工数据的获取、添加、修改和删除。按照REST设计原则,我们将被操作的员工信息体现为某种网络资源,而操作类型最好与相应的HTTP方法相匹配。在操作方法中针对资源的操作类型与HTTP方法之间的匹配是通过应用在 它们上面的WebGetAttribute和WebInvokeAttribute特性来体现。

    WebGetAttribute针对GET方法,而其他的HTTP方法则通过WebInvokeAttribute的Method属性来体现。在 IEmployeesService中,两个用于获取员工信息GetAll和Get方法均应用了WebGetAttribute特性,而其他的 Create、Update和Delete方法在应用了WebInvokeAttribute特性,并且其Method属性被分别设置为PUT、POST 和DELETE。

    WebGetAttribute和WebInvokeAttribute和均具有相同的属性UriTemplate,该属性用于定义作为最终操作 URI的模板。我们不仅可以通过UriTemplate属性为操作指定一个相对于终结点地址的静态路径,还可以通过占位符实现路径中的动态部分与参数之间 的映射。

    同样以定义在契约接口IEmployeesService中的5个操作方法为例,如果终结点地址为http://127.0.0.1:3721 /employees,由于用于返回所有员工列表的GetAll操作的UriTemplate被设置“All”,所以其地址为http: //127.0.0.1:3721/employees。用于返回指定员工ID的Get操作的UriTemplate被设置成“{id}”,意味着我们直接在表示请求地址的URI中指定员工的ID,而它会自动映射为该操作方法的参数id。用于删除某个指定员工的Delete操作具有相同的 UriTemplate设置,而用于创建添加新员工和修改现有员工信息的Create和Update操作,由于作为参数的Employee对象具有ID属 性,所以直接采用终结点地址。

  • 相关阅读:
    错题集知识(持续更新)
    Java 初始化过程
    java基础自定义测试异常类
    1049 数列的片段和 (20 分)
    1044 火星数字 (20 分)
    1048 数字加密(20分)
    js 全选反选和全不选
    robust programmings
    gdb define command
    好久没有破解别人的无线了, 重温一下
  • 原文地址:https://www.cnblogs.com/atuotuo/p/5457256.html
Copyright © 2011-2022 走看看