创建一个WCF Rest Service

1、创建一个WCF Service Application



2、创建一个实体对象Student,用作数据传输的载体,下面是Student.cs的内容

[csharp]  view plain copy print ?
  1. using System.Runtime.Serialization;  
  2. namespace WCFRest  
  3. {  
  4.     /// <summary>  
  5.     /// DataContract 数据契约:服务端和客户端之间要传送的自定义数据类型  
  6.     /// </summary>  
  7.     [DataContract]  
  8.     public class Student  
  9.     {  
  10.         /// <summary>  
  11.         /// 在数据传送过程中,只有成员变量可以被传送而成员方法不可以。  
  12.         /// 并且只有当成员变量加上DataMember时才可以被序列进行数据传输,  
  13.         /// 如果不加DataMember,客户端将无法获得该属性的任何信息  
  14.         /// </summary>  
  15.         [DataMember]  
  16.         public int Id { getset; }  
  17.         [DataMember]  
  18.         public string Name { getset; }  
  19.     }  
  20. }  

同时我们创建一个类,用来模拟数据库的存储

[csharp]  view plain copy print ?
  1. using System.Collections.Generic;  
  2.   
  3. namespace WCFRest  
  4. {  
  5.     public class UserList  
  6.     {  
  7.         private static readonly UserList _Instance = new UserList();  
  8.         private UserList() { }  
  9.   
  10.         public static UserList Instance  
  11.         {  
  12.             get { return _Instance; }  
  13.         }  
  14.   
  15.         public IList<Student> Users  
  16.         {  
  17.             get { return _Users; }  
  18.         }  
  19.   
  20.         private IList<Student> _Users = new List<Student>{  
  21.             new Student {Id = 1, Name = "张三" },  
  22.             new Student {Id = 2, Name = "李四" },  
  23.             new Student {Id = 3, Name = "王五" }  
  24.         };  
  25.     }  
  26. }  


3、创建服务契约

下面我们在项目添加一个WCF Service


首先修改IStudnetService接口,配置Rest的URL路径

[csharp]  view plain copy print ?
  1. using System.Collections.Generic;  
  2. using System.ServiceModel;  
  3. using System.ServiceModel.Web;  
  4.   
  5. namespace WCFRest  
  6. {  
  7.     [ServiceContract]  
  8.     public interface IStudentService  
  9.     {  
  10.         [OperationContract]  
  11.         [WebInvoke(Method = "GET",  
  12.            RequestFormat = WebMessageFormat.Json,  
  13.            ResponseFormat = WebMessageFormat.Json,  
  14.            UriTemplate = "GetStudentById/Id={Id}"  
  15.          )]  
  16.         Student GetStudentById(string Id);  
  17.   
  18.         [OperationContract]  
  19.         [WebInvoke(Method = "GET",  
  20.            RequestFormat = WebMessageFormat.Json,  
  21.            ResponseFormat = WebMessageFormat.Json,  
  22.            UriTemplate = "GetStudentList"  
  23.          )]  
  24.         IList<Student> GetStudentList();  
  25.     }  
  26. }  

4、修改StudentService类,实现Rest方法

[csharp]  view plain copy print ?
  1. using System.Collections.Generic;  
  2.   
  3. namespace WCFRest  
  4. {  
  5.     public class StudentService : IStudentService  
  6.     {  
  7.           
  8.         public Student GetStudentById(string Id)  
  9.         {  
  10.             return StudentList.Instance.Users[int.Parse(Id)];  
  11.         }  
  12.   
  13.         public IList<Student> GetStudentList()  
  14.         {  
  15.             return StudentList.Instance.Users;  
  16.         }  
  17.     }  
  18. }  

5、配置Service和Behavior

在Web.Config中配置我们的Rest服务

[html]  view plain copy print ?
  1. <system.serviceModel>  
  2.    <services>  
  3.      <service name="WCFRest.StudentService" behaviorConfiguration="serviceBehavior">  
  4.        <endpoint address="" binding="webHttpBinding" contract="WCFRest.IStudentService"   
  5.                  behaviorConfiguration="web"></endpoint>  
  6.      </service>  
  7.    </services>  
  8.    <behaviors>  
  9.      <serviceBehaviors>  
  10.        <behavior name="serviceBehavior">  
  11.          <serviceMetadata httpGetEnabled="true"/>  
  12.          <serviceDebug includeExceptionDetailInFaults="false"/>  
  13.        </behavior>  
  14.        <behavior>  
  15.          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>  
  16.          <serviceDebug includeExceptionDetailInFaults="false"/>  
  17.        </behavior>  
  18.      </serviceBehaviors>  
  19.      <endpointBehaviors>  
  20.        <behavior name="web">  
  21.          <webHttp/>  
  22.        </behavior>  
  23.      </endpointBehaviors>  
  24.    </behaviors>  
  25.    <protocolMapping>  
  26.      <add binding="basicHttpsBinding" scheme="https" />  
  27.    </protocolMapping>  
  28.    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />  
  29.  </system.serviceModel>  

OK,下面测试一下我们的程序,右击Student.svc文件,选择View In Browser,我们将会看到下面的运行结果



然后我们在url后面加上我们定义的Rest地址就可以访问Rest服务了





说明:本文绝大部分内容摘自下面文章

http://www.topwcftutorials.net/2013/09/simple-steps-for-restful-service.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值