写在前面
公司有个项目,需要和sharepoint集成,用到了restful,在网上查了一下,wcf方式来实现,还是很方便的,就采用了这种方式,至于web api,没研究过,由于接口急着用,就采用wcf了,用起来也比较顺手。
概念
什么是rest?
REST 描述了一个架构样式的互联系统(如 Web 应用程序)。REST 约束条件作为一个整体应用时,将生成一个简单、可扩展、有效、安全、可靠的架构。由于它简便、轻量级以及通过 HTTP 直接传输数据的特性,RESTful Web 服务成为基于 SOAP 服务的一个最有前途的替代方案。用于 web 服务和动态 Web 应用程序的多层架构可以实现可重用性、简单性、可扩展性和组件可响应性的清晰分离。开发人员可以轻松使用 Ajax 和 RESTful Web 服务一起创建丰富的界面。
REST 指的是一组架构约束条件和原则。满足这些约束条件和原则的应用程序或设计就是 RESTful。
一个例子
新建一个wcf项目。代码如下:
契约
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. [ServiceContract] public interface IUserService { /// <summary> /// 获得所有的用户信息 /// </summary> /// <returns>json或者xml</returns> [OperationContract] [WebGet(UriTemplate = "api/query", ResponseFormat = WebMessageFormat.Xml)] List<UserInfo> QueryList(); /// <summary> /// 根据id查询用户信息 /// </summary> /// <param name="id"></param> /// <returns></returns> [OperationContract] [WebGet(UriTemplate = "api/querybyid/{id}", ResponseFormat = WebMessageFormat.Json)] UserInfo Query(string id); /// <summary> /// 根据名字查询 /// </summary> /// <param name="name"></param> /// <returns></returns> [OperationContract] [WebInvoke(UriTemplate = "api/querybyname/{name}", Method = "GET", ResponseFormat = WebMessageFormat.Xml)] UserInfo QueryByName(string name); /// <summary> /// 根据编号删除用户信息 /// </summary> /// <param name="id"></param> /// <returns></returns> [OperationContract] [WebInvoke(UriTemplate = "api/delete/{id}", Method = "DELETE", ResponseFormat = WebMessageFormat.Xml)] bool Delete(string id); /// <summary> /// 添加用户信息 /// </summary> /// <param name="userInfo"></param> /// <returns></returns> [OperationContract] [WebInvoke(UriTemplate = "api/add", Method = "POST", ResponseFormat = WebMessageFormat.Xml)] bool Insert(UserInfo userInfo); /// <summary> /// 更新用户信息 /// </summary> /// <param name="userInfo"></param> /// <returns></returns> [OperationContract] [WebInvoke(UriTemplate = "api/modify", Method = "PUT", ResponseFormat = WebMessageFormat.Xml)] bool Update(UserInfo userInfo); }
服务
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging. public class UserService : IUserService { /// <summary> /// 获得所有的用户信息 /// </summary> /// <returns>json或者xml</returns> public List<UserInfo> QueryList() { return new List<UserInfo>() { new UserInfo() { ID = 1, Name = "wofly", Age = 22, Birthday = DateTime.Now, Gender = true }, new UserInfo() { ID = 2, Name = "san zhang", Age = 21, Birthday = DateTime.Now, Gender = true }, new UserInfo() { ID = 3, Name = "wukong sun", Age = 23, Birthday = DateTime.Now, Gender = false }, new UserInfo() { ID = 4 Name = "zi ma", Age = 45, Birthday = DateTime.Now, Gender = true } }; } /// <summary> /// 根据id查询用户信息 /// </summary> /// <param name="id"></param> /// <returns></returns> public UserInfo Query(string id) { if (string.IsNullOrEmpty(id)) { throw new ArgumentNullException("id"); } var users = QueryList(); int iId = Convert.ToInt32(id); return users.Where(x => x.ID == iId).FirstOrDefault(); } /// <summary> /// 根据编号删除用户信息 /// </summary> /// <param name="id"></param> /// <returns></returns> public bool Delete(string id) { if (string.IsNullOrEmpty(id)) { throw new ArgumentNullException("id"); } //当前操作上下文 WebOperationContext woc = WebOperationContext.Current; //状态码 woc.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK; try { var users = QueryList(); int iId = Convert.ToInt32(id); var user = users.Where(x => x.ID == iId).FirstOrDefault(); return users.Remove(user); } catch (Exception ex) { throw ex; } } /// <summary> /// 添加用户信息 /// </summary> /// <param name="userInfo"></param> /// <returns></returns> public bool Insert(UserInfo userInfo) { throw new NotImplementedException(); } /// <summary> /// 更新用户信息 /// </summary> /// <param name="userInfo"></param> /// <returns></returns> public bool Update(UserInfo userInfo) { throw new NotImplementedException(); } /// <summary> /// 根据名字查询 /// </summary> /// <param name="name"></param> /// <returns></returns> public UserInfo QueryByName(string name) { var users = QueryList(); return users.Where(x => x.Name.Equals(name)).FirstOrDefault(); } }
用户信息类
/// <summary> /// 用户信息类 /// </summary> [DataContract] public class UserInfo { /// <summary> /// 编号 /// </summary> [DataMember] public int ID { set; get; } /// <summary> /// 名字 /// </summary> [DataMember] public string Name { set; get; } /// <summary> /// 性别 /// </summary> [DataMember] public bool Gender { set; get; } /// <summary> /// 年龄 /// </summary> [DataMember] public int Age { set; get; } /// <summary> /// 生日 /// </summary> [DataMember] public DateTime Birthday { set; get; } }
通过 WCF 4.0 里创建的 WCF Service Application 发布REST服务很简单,只需要在 svc 的 Markup 里加上 Factory:
原
<%@ ServiceHost Language="C#" Debug="true" Service="Wolfy.WCFRestfuleDemo.UserService" CodeBehind="UserService.svc.cs" %>
修改后
<%@ ServiceHost Language="C#" Debug="true" Service="Wolfy.WCFRestfuleDemo.UserService" CodeBehind="UserService.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>
修改前,浏览svc服务
修改后
注意:UriTemplate 路径段的变量类型必须为“字符串”。
修改后,浏览svc服务
到这里,你应该发现了,WCF的Metadata不能访问了,也就说不能访问svc的wsdl了。
测试
在浏览器中输入:http://localhost:21074/UserService.svc/api/query
到这里已经成功了,那么我们访问一下id为1用户信息
http://localhost:21074/UserService.svc/api/querybyid/1
{"Age":22,"Birthday":"\/Date(1433419746141+0800)\/","Gender":true,"ID":1,"Name":"wofly"}
路由方式访问
看到上面的url,很像mvc的路由,那有没有办法修改成路由访问方式呢?答案是肯定的。只需在全局处理程序中注册路由就可以了。
public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { //注册路由 System.Web.Routing.RouteTable.Routes.Add( new System.ServiceModel.Activation.ServiceRoute( "userInfo", new System.ServiceModel.Activation.WebServiceHostFactory(), typeof(UserService) )); } }
使用注册的路由访问服务
http://localhost:21074/userInfo/api/query
总结
这里实现了restful风格的wcf的查询操作,关于增删改(post,delete,put)将在后面的文章中说明。
参考文章:
http://blog.csdn.net/fangxing80/article/details/6235662