利用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月月考生物试卷
网友关注
- 如果以后
- 车辆交易手续流程、费用Word
- 2015届河北省“五个一名校联盟” 高三教学质量监测二语文试卷答案
- 战略制定的三项任务-37页Word
- (名词,代词)Word
- 战略制定的三项任务-37页Word
- PN结Word
- 第十章 成本控制Word
- 人力资源招聘体系建设Word
- 中药学基础知识Word
- 排球传球课教案,高中
- 第2课_贞观之治(中华书局版)Word
- 20141222涿州大石桥综合项目前期策划方案Word
- 阀门知识简介(中)Word
- 如果你老公在部队
- 微笑,唱生活的歌
- 第四章 食品微生物检验样品的制备Word
- 适合幼儿园的消防安全预案样本
- 战略制定的三项任务-37页Word
- 战略规划流程与方法-麦肯锡Word
- 培训师的百宝箱课堂游戏实战经典(44页)Word
- 第十章 成本控制Word
- 战略规划流程与方法-麦肯锡Word
- 阀门知识简介(下)Word
- 顾客服务Word
- 文心Word
- 章燕+幼儿园早操活动方案
- 人工湿地处理污水节能技术Word
- PN结Word
- 如果我是男孩
网友关注视频
- 冀教版小学数学二年级下册第二周第2课时《我们的测量》宝丰街小学庞志荣.mp4
- 北师大版数学四年级下册第三单元第四节街心广场
- 【部编】人教版语文七年级下册《老山界》优质课教学视频+PPT课件+教案,安徽省
- 【部编】人教版语文七年级下册《泊秦淮》优质课教学视频+PPT课件+教案,辽宁省
- 【部编】人教版语文七年级下册《老山界》优质课教学视频+PPT课件+教案,安徽省
- 沪教版牛津小学英语(深圳用) 四年级下册 Unit 7
- 每天日常投篮练习第一天森哥打卡上脚 Nike PG 2 如何调整运球跳投手感?
- 外研版英语三起6年级下册(14版)Module3 Unit2
- 苏科版八年级数学下册7.2《统计图的选用》
- 七年级英语下册 上海牛津版 Unit9
- 化学九年级下册全册同步 人教版 第25集 生活中常见的盐(二)
- 19 爱护鸟类_第一课时(二等奖)(桂美版二年级下册)_T3763925
- 冀教版小学数学二年级下册第二单元《租船问题》
- 外研版八年级英语下学期 Module3
- 【部编】人教版语文七年级下册《泊秦淮》优质课教学视频+PPT课件+教案,天津市
- 冀教版小学英语四年级下册Lesson2授课视频
- 《小学数学二年级下册》第二单元测试题讲解
- 二年级下册数学第二课
- 人教版二年级下册数学
- 外研版英语七年级下册module3 unit2第一课时
- 《空中课堂》二年级下册 数学第一单元第1课时
- 冀教版英语三年级下册第二课
- 【部编】人教版语文七年级下册《逢入京使》优质课教学视频+PPT课件+教案,辽宁省
- 小学英语单词
- 第五单元 民族艺术的瑰宝_16. 形形色色的民族乐器_第一课时(岭南版六年级上册)_T3751175
- 8.对剪花样_第一课时(二等奖)(冀美版二年级上册)_T515402
- 七年级英语下册 上海牛津版 Unit5
- 【获奖】科粤版初三九年级化学下册第七章7.3浓稀的表示
- 外研版英语三起5年级下册(14版)Module3 Unit2
- 30.3 由不共线三点的坐标确定二次函数_第一课时(市一等奖)(冀教版九年级下册)_T144342
精品推荐
- 2016-2017学年高一语文人教版必修一+模块学业水平检测试题(含答案)
- 广西钦州市高新区2017届高三11月月考政治试卷
- 浙江省湖州市2016-2017学年高一上学期期中考试政治试卷
- 浙江省湖州市2016-2017学年高二上学期期中考试政治试卷
- 辽宁省铁岭市协作体2017届高三上学期第三次联考政治试卷
- 广西钦州市钦州港区2016-2017学年高二11月月考政治试卷
- 广西钦州市钦州港区2017届高三11月月考政治试卷
- 广西钦州市钦州港区2016-2017学年高一11月月考政治试卷
- 广西钦州市高新区2016-2017学年高二11月月考政治试卷
- 广西钦州市高新区2016-2017学年高一11月月考政治试卷
分类导航
- 互联网
- 电脑基础知识
- 计算机软件及应用
- 计算机硬件及网络
- 计算机应用/办公自动化
- .NET
- 数据结构与算法
- Java
- SEO
- C/C++资料
- linux/Unix相关
- 手机开发
- UML理论/建模
- 并行计算/云计算
- 嵌入式开发
- windows相关
- 软件工程
- 管理信息系统
- 开发文档
- 图形图像
- 网络与通信
- 网络信息安全
- 电子支付
- Labview
- matlab
- 网络资源
- Python
- Delphi/Perl
- 评测
- Flash/Flex
- CSS/Script
- 计算机原理
- PHP资料
- 数据挖掘与模式识别
- Web服务
- 数据库
- Visual Basic
- 电子商务
- 服务器
- 搜索引擎优化
- 存储
- 架构
- 行业软件
- 人工智能
- 计算机辅助设计
- 多媒体
- 软件测试
- 计算机硬件与维护
- 网站策划/UE
- 网页设计/UI
- 网吧管理