标签:
Representational State Transfer(REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格。
一、按一下结构创建项目,其中WCF.REST.Services项目选用WCF REST Service Template 40(CS)模板
Contracts引用System.ServiceModel和System.ServiceModel.Web
Services引用Contracts
二、在Contracts项目中创建接口个传输类:传输类会在调用的客户端等地方使用,建议使用全小写,以免调用时产生疏忽。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
using System; namespace WCF.REST.Contracts { /// <summary> /// 该类用于传输 /// </summary> public class item { public int id { get ; set ; } public string name { get ; set ; } public decimal money { get ; set ; } public DateTime birthday { get ; set ; } public int number { get ; set ; } } } |
三、在Contracts项目中创建协议接口:在这里定义接口,方便维护,将接口和实现类进行分离。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
using System.Collections.Generic; using System.ServiceModel; using System.ServiceModel.Web; namespace WCF.REST.Contracts { /// <summary> /// 服务协议 /// </summary> [DataContractFormat] [ServiceContract] public interface IItem { [WebGet(UriTemplate = "" )] List<item> GetCollection(); [WebInvoke(UriTemplate = "" , Method = "POST" )] item Create(item instance); [WebGet(UriTemplate = "{id}" )] item Get( string id); [WebInvoke(UriTemplate = "{id}" , Method = "PUT" , BodyStyle = WebMessageBodyStyle.WrappedRequest)] item Update( string id, item instance); [WebInvoke(UriTemplate = "{id}" , Method = "DELETE" )] void Delete( string id); } } |
四、在Services中创建逻辑类,用于存储列表:真实项目时这个类可以单独放在业务逻辑层中。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
using System.Collections.Generic; using WCF.REST.Contracts; namespace WCF.REST.Services { /// <summary> /// 服务逻辑 /// </summary> public static class ItemsBL { public static List<item> list = new List<item>(); } } |
五、在Services中创建服务类:这是服务的具体实现。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
using System.Collections.Generic; using System.Linq; using System.ServiceModel.Activation; using WCF.REST.Contracts; namespace WCF.REST.Services { /// <summary> /// 服务实现 /// </summary> [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class Items : IItem { public List<item> GetCollection() { return ItemsBL.list; } public item Create(item instance) { lock (ItemsBL.list) { if (ItemsBL.list.Count > 0) instance.id = ItemsBL.list.Max(p => p.id) + 1; else instance.id = 1; } ItemsBL.list.Add(instance); return instance; } public item Get( string id) { return ItemsBL.list.FirstOrDefault(p => p.id == int .Parse(id)); } public item Update( string id, item instance) { int iid = int .Parse(id); item Result = null ; lock (ItemsBL.list) { if (ItemsBL.list.Count(p => p.id == iid) > 0) { ItemsBL.list.Remove(ItemsBL.list.FirstOrDefault(p => p.id == iid)); ItemsBL.list.Add(instance); Result = instance; } } return Result; } public void Delete( string id) { int iid = int .Parse(id); lock (ItemsBL.list) { if (ItemsBL.list.Count(p => p.id == iid) > 0) { ItemsBL.list.Remove(ItemsBL.list.FirstOrDefault(p => p.id == iid)); } } } } } |
六、修改Global文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using System; using System.ServiceModel.Activation; using System.Web; using System.Web.Routing; namespace WCF.REST.Services { public class Global : HttpApplication { void Application_Start( object sender, EventArgs e) { RegisterRoutes(); } private void RegisterRoutes() { RouteTable.Routes.Add( new ServiceRoute( "Items" , new WebServiceHostFactory(), typeof (Items))); } } } |
七、运行服务在地址栏后添加 /items/help 后看到如下界面
ok至此服务部分完成。
标签:
原文地址:http://www.cnblogs.com/Rozdy/p/4232241.html