RESTful 服务示例

WCF服务轻量级服务,可供JS调用

返回值格式:XML、Json

工程结构:

示例代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 
 6 namespace RESTfulWebServices
 7 {
 8     public class UserInfo
 9     {
10         public string Name { get; set; }
11         public int Age { get; set; }
12         public string Sex { get; set; }
13     }
14 }
实体类
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Runtime.Serialization;
 5 using System.ServiceModel;
 6 using System.Text;
 7 using System.ServiceModel.Web;
 8 
 9 namespace RESTfulWebServices
10 {
11     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
12     [ServiceContract]
13     public interface IService1
14     {
15         [OperationContract]
16         [WebGet(UriTemplate = "GetList/{name1}/{name2}",
17            BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
18         List<UserInfo> GetList(string name1, string name2);
19 
20         [OperationContract]
21         [WebGet(UriTemplate = "GetUserInfo",
22            BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
23         UserInfo GetUserInfo();
24     }
25 }
服务契约
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Runtime.Serialization;
 5 using System.ServiceModel;
 6 using System.Text;
 7 
 8 namespace RESTfulWebServices
 9 {
10     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
11     public class Service1 : IService1
12     {
13         public List<UserInfo> GetList(string name1, string name2)
14         {
15             List<UserInfo> list = new List<UserInfo>();
16             UserInfo u1 = new UserInfo();
17             u1.Age = 39;
18             u1.Name = name1;
19             u1.Sex = "";
20             list.Add(u1);
21 
22             UserInfo u2 = new UserInfo();
23             u2.Age = 29;
24             u2.Name = name2;
25             u2.Sex = "";
26             list.Add(u2);
27 
28             return list;
29         }
30 
31         public UserInfo GetUserInfo()
32         {
33             UserInfo u2 = new UserInfo();
34             u2.Age = 59;
35             u2.Name = "王老五";
36             u2.Sex = "";
37             return u2;
38         }
39     }
40 }
服务实现
 1 <?xml version="1.0"?>
 2 <configuration>
 3   <system.web>
 4     <compilation debug="true" targetFramework="4.0" />
 5   </system.web>
 6   <system.serviceModel>
 7     <services>
 8       <service behaviorConfiguration="GetPostBehavior" name="RESTfulWebServices.Service1">
 9         <endpoint address="" behaviorConfiguration="GetPostEndBehaviors" binding="webHttpBinding" 
10                   contract="RESTfulWebServices.IService1">
11         </endpoint>
12         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
13       </service>
14     </services>
15     <behaviors>
16       <endpointBehaviors>
17         <behavior name="GetPostEndBehaviors">
18           <webHttp />
19         </behavior>
20       </endpointBehaviors>
21       <serviceBehaviors>
22         <behavior name="GetPostBehavior">
23           <serviceMetadata httpGetEnabled="true" />
24           <serviceDebug includeExceptionDetailInFaults="false" />
25         </behavior>
26       </serviceBehaviors>
27     </behaviors>
28     <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
29   </system.serviceModel>
30 </configuration>
配置文件

运行结果:

无参服务调用方式:地址+方法名 http://localhost:1768/Service1.svc/GetUserInfo

XML格式返回值:

带参数服务调用方式:地址+方法名+参数1+参数2+参数N  http://localhost:1768/Service1.svc/GetList/Jim/Tom

Json格式返回值:

示例源码下载:RESTfulWebServices.rar

RESTful 服务(配备 WCF)介绍

 

原文地址:https://www.cnblogs.com/yf2011/p/4704230.html