利用WCF创建简单的RESTFul Service

利用WCF创建简单的RESTFul Service

ID:42583637

大小:523.50 KB

页数:13页

时间:2019-09-18

利用WCF创建简单的RESTFul Service_第1页
利用WCF创建简单的RESTFul Service_第2页
利用WCF创建简单的RESTFul Service_第3页
利用WCF创建简单的RESTFul Service_第4页
利用WCF创建简单的RESTFul Service_第5页
资源描述:

《利用WCF创建简单的RESTFul Service》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、利用WCF创建简单的RESTFulService***服务端程序见WCFcompany1、用VS2013创建一个WCF的工程,如下图所示:2、对IService1.cs文件进行修改,在里面添加服务契约和数据契约,具体代码如下所示:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.Serialization;usingSystem.ServiceModel;usingSystem.ServiceModel.Web;u

2、singSystem.Text;namespaceWCFcompany{//注意:使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。//定义服务契约[ServiceContract]publicinterfaceIService1{//TODO:在此添加您的服务操作[OperationContract][WebGet(UriTemplate="getcustomer/{id}",RequestFormat=WebMessageFormat.Json,ResponseFormat=

3、WebMessageFormat.Json)]CustomerGetCustomerById(stringid);[OperationContract][WebGet(UriTemplate="getallcustomer",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]ListGetAllCustomer();[OperationContract][WebInvoke(Method="POST",Uri

4、Template="addcustomer",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]boolAddCustomer(Customercust);[OperationContract]//如果有多个参数要使用BodyStyle=WebMessageBodyStyle.Wrapped//[WebInvoke(Method="PUT",UriTemplate="updatecustomer",RequestFormat=W

5、ebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped)][WebInvoke(Method="PUT",UriTemplate="updatecustomer",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]boolUpdateCustomer(Customercust);[Operati

6、onContract][WebInvoke(Method="DELETE",UriTemplate="deletecustomer/{id}",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]boolDeleteCustomer(stringid);}//使用下面示例中说明的数据约定将复合类型添加到服务操作。//定义数据契约[DataContract]publicclassCustomer{[DataMember]public

7、stringCust_id{get;set;}[DataMember]publicstringCust_name{get;set;}[DataMember]publicstringAddr{get;set;}[DataMember]publicstringTel_no{get;set;}[DataMember]publicstringZip{get;set;}}}注意:(1)WCFREST中通过[WebGet]、[WebInvoke]特性很好的利用了HTTP的GET/POST/PUT/DELETE方式,绑定到服务的不同方法上

8、。比如GET方法不用客户端提供太多数据,正适合查询只提供主键或者查询字段的场景。POST则适合数据的插入,PUT则应用在数据更新,DELETE则直接用在数据删除上。(2)当请求为Get时 一般用来查询数据,使用[WebGet] ;而增加、修改、删除时,则使用[WebInvoke],并用Me

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。