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属 性,所以直接采用终结点地址。

转载于:https://www.cnblogs.com/atuotuo/p/5457256.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值