教育资源为主的文档平台

当前位置: 查字典文档网> 所有文档分类> > 计算机软件及应用> 利用WCF创建简单的RESTFul Service

利用WCF创建简单的RESTFul Service

上传者:付长东
|
上传时间:2015-05-04
|
次下载

利用WCF创建简单的RESTFul Service

利用WCF创建简单的RESTFul Service 1、 用VS2013创建一个WCF的工程,如下图所示:

2、 对IService1.cs文件进行修改,在里面添加服务契约和数据契约,具体代码如下所示:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

namespace WCFcompany

{

// 注意: 使用“重构

内容需要下载文档才能查看

”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。

[ServiceContract]

public interface IService1

{

// TODO: 在此添加您的服务操作

[OperationContract]

[WebGet(UriTemplate = "getcustomer/{id}", RequestFormat =

WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]

Customer GetCustomerById(string id);

[OperationContract]

[WebGet(UriTemplate = "getallcustomer", RequestFormat =

WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]

List<Customer> GetAllCustomer();

[OperationContract]

[WebInvoke(Method = "POST", UriTemplate = "addcustomer", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]

bool AddCustomer(Customer cust);

[OperationContract]

[WebInvoke(Method = "PUT", UriTemplate = "updatecustomer", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]

bool UpdateCustomer(string id,string name);

[OperationContract]

[WebInvoke(Method = "DELETE", UriTemplate = "deletecustomer",

RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] bool DeleteCustomer(string id);

}

// 使用下面示例中说明的数据约定将复合类型添加到服务操作。

[DataContract]

public class Customer

{

string cust_id ; string cust_name;

string addr;

string tel_no;

string zip;

[DataMember]

public string Cust_id

{

get { return cust_id; }

set { cust_id = value; }

}

[DataMember]

public string Cust_name

{

get { return cust_name; }

set { cust_name = value; }

}

[DataMember]

public string Addr

{

get { return addr; }

set { addr = value; }

}

[DataMember]

public string Tel_no

{

get { return tel_no; }

set { tel_no = value; }

}

[DataMember]

public string Zip

{

get { return zip; }

set { zip = value; }

}

}

}

3、 在Service1.svc.cs文件中实现IService1接口,代码如下: using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Activation;

using System.ServiceModel.Web;

using System.Text;

using System.Data;

using System.Data.SqlClient;

using SqlServerDAL;

namespace WCFcompany

{

// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。

// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 Service1.svc 或 Service1.svc.cs,然后开始调试。

//以下两个特性是一定要加上去的,否则报错

[AspNetCompatibilityRequirements(RequirementsMode =

AspNetCompatibilityRequirementsMode.Allowed)]

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class Service1 : IService1

{

private DataTable SqlConnHelpGetDataMethod(string sql)

{

CnnHelper.ConnectionSql();

CnnHelper.IsOpenHelp();

http://wendang.chazidian.comeCommand(sql, CommandType.Text);

DataTable dt = http://wendang.chazidian.comeSqlDataReader(); return dt;

}

private int SqlConnHelpSetDataMethod(string sql)

{

CnnHelper.ConnectionSql();

CnnHelper.IsOpenHelp();

http://wendang.chazidian.comeCommand(sql, CommandType.Text);

return http://wendang.chazidian.comeExecuteNonQuery();

}

public Customer GetCustomerById(string id)

{

//string sql = "select * from customer where cust_id='" + id + "'"; //Customer cust = new Customer();

//DataTable dt = SqlConnHelpGetDataMethod(sql);

//if (dt != null && dt.Rows.Count == 1)

//{

// cust.Cust_id = dt.Rows[0][0].ToString();

// cust.Cust_name = dt.Rows[0][1].ToString();

// cust.Addr = dt.Rows[0][2].ToString();

// cust.Tel_no = dt.Rows[0][3].ToString();

// cust.Zip = dt.Rows[0][4].ToString();

//}

//else cust = null;

//return cust;

Customer cust = new Customer();

cust.Cust_id = "C0001";

cust.Cust_name = "Jack";

cust.Addr = "asd";

cust.Tel_no = "253y565u5";

cust.Zip = "210940923";

return cust;

}

public List<Customer> GetAllCustomer()

{

//List<Customer> custList = new List<Customer>();

//string sql = "select * from customer";

//DataTable dt = SqlConnHelpGetDataMethod(sql);

//if (dt != null && dt.Rows.Count != 0)

//{

// for (int i = 0; i < dt.Rows.Count; i++)

// {

// Customer cust = new Customer();

// cust.Cust_id = dt.Rows[i][0].ToString();

// cust.Cust_name = dt.Rows[i][1].ToString();

// cust.Addr = dt.Rows[i][2].ToString();

// cust.Tel_no = dt.Rows[i][3].ToString();

// cust.Zip = dt.Rows[i][4].ToString();

// custList.Add(cust);

// }

//}

//else custList = null;

//return custList;

List<Customer> custList = new List<Customer>();

Customer cust0 = new Customer() { Cust_id="C0001", Cust_name="Tom", Addr="asfasdg", Tel_no="13345569", Zip="1233234" };

Customer cust1 = new Customer() { Cust_id = "C0002", Cust_name = "Jack", Addr = "sdgsdgg", Tel_no = "15634579", Zip = "56456776" };

custList.Add(cust0);

custList.Add(cust1);

return custList;

}

public bool AddCustomer(Customer cust)

{

string sql = "insert into customer values('" + cust.Cust_id + "','" + cust.Cust_name + "','" + cust.Addr + "','" + cust.Tel_no + "','" + cust.Zip + "')";

版权声明:此文档由查字典文档网用户提供,如用于商业用途请与作者联系,查字典文档网保持最终解释权!

下载文档

热门试卷

2016年四川省内江市中考化学试卷
广西钦州市高新区2017届高三11月月考政治试卷
浙江省湖州市2016-2017学年高一上学期期中考试政治试卷
浙江省湖州市2016-2017学年高二上学期期中考试政治试卷
辽宁省铁岭市协作体2017届高三上学期第三次联考政治试卷
广西钦州市钦州港区2016-2017学年高二11月月考政治试卷
广西钦州市钦州港区2017届高三11月月考政治试卷
广西钦州市钦州港区2016-2017学年高一11月月考政治试卷
广西钦州市高新区2016-2017学年高二11月月考政治试卷
广西钦州市高新区2016-2017学年高一11月月考政治试卷
山东省滨州市三校2017届第一学期阶段测试初三英语试题
四川省成都七中2017届高三一诊模拟考试文科综合试卷
2017届普通高等学校招生全国统一考试模拟试题(附答案)
重庆市永川中学高2017级上期12月月考语文试题
江西宜春三中2017届高三第一学期第二次月考文科综合试题
内蒙古赤峰二中2017届高三上学期第三次月考英语试题
2017年六年级(上)数学期末考试卷
2017人教版小学英语三年级上期末笔试题
江苏省常州西藏民族中学2016-2017学年九年级思想品德第一学期第二次阶段测试试卷
重庆市九龙坡区七校2016-2017学年上期八年级素质测查(二)语文学科试题卷
江苏省无锡市钱桥中学2016年12月八年级语文阶段性测试卷
江苏省无锡市钱桥中学2016-2017学年七年级英语12月阶段检测试卷
山东省邹城市第八中学2016-2017学年八年级12月物理第4章试题(无答案)
【人教版】河北省2015-2016学年度九年级上期末语文试题卷(附答案)
四川省简阳市阳安中学2016年12月高二月考英语试卷
四川省成都龙泉中学高三上学期2016年12月月考试题文科综合能力测试
安徽省滁州中学2016—2017学年度第一学期12月月考​高三英语试卷
山东省武城县第二中学2016.12高一年级上学期第二次月考历史试题(必修一第四、五单元)
福建省四地六校联考2016-2017学年上学期第三次月考高三化学试卷
甘肃省武威第二十三中学2016—2017学年度八年级第一学期12月月考生物试卷

网友关注视频

苏教版二年级下册数学《认识东、南、西、北》
【部编】人教版语文七年级下册《泊秦淮》优质课教学视频+PPT课件+教案,天津市
冀教版小学数学二年级下册第二周第2课时《我们的测量》宝丰街小学庞志荣
【部编】人教版语文七年级下册《逢入京使》优质课教学视频+PPT课件+教案,安徽省
外研版英语三起5年级下册(14版)Module3 Unit1
【部编】人教版语文七年级下册《老山界》优质课教学视频+PPT课件+教案,安徽省
8.对剪花样_第一课时(二等奖)(冀美版二年级上册)_T515402
沪教版牛津小学英语(深圳用) 五年级下册 Unit 10
北师大版小学数学四年级下册第15课小数乘小数一
七年级英语下册 上海牛津版 Unit9
【部编】人教版语文七年级下册《逢入京使》优质课教学视频+PPT课件+教案,辽宁省
第8课 对称剪纸_第一课时(二等奖)(沪书画版二年级上册)_T3784187
沪教版牛津小学英语(深圳用) 五年级下册 Unit 7
北师大版数学四年级下册第三单元第四节街心广场
北师大版数学 四年级下册 第三单元 第二节 小数点搬家
二年级下册数学第三课 搭一搭⚖⚖
30.3 由不共线三点的坐标确定二次函数_第一课时(市一等奖)(冀教版九年级下册)_T144342
第五单元 民族艺术的瑰宝_16. 形形色色的民族乐器_第一课时(岭南版六年级上册)_T3751175
冀教版小学数学二年级下册1
苏科版数学 八年级下册 第八章第二节 可能性的大小
沪教版牛津小学英语(深圳用) 四年级下册 Unit 7
外研版英语三起6年级下册(14版)Module3 Unit1
外研版英语七年级下册module3 unit2第一课时
人教版历史八年级下册第一课《中华人民共和国成立》
苏科版八年级数学下册7.2《统计图的选用》
第4章 幂函数、指数函数和对数函数(下)_六 指数方程和对数方程_4.7 简单的指数方程_第一课时(沪教版高一下册)_T1566237
冀教版英语五年级下册第二课课程解读
【获奖】科粤版初三九年级化学下册第七章7.3浓稀的表示
【部编】人教版语文七年级下册《泊秦淮》优质课教学视频+PPT课件+教案,广东省
外研版英语七年级下册module1unit3名词性物主代词讲解