1、創(chuàng)建一個(gè)WCF Service Application
2、創(chuàng)建一個(gè)實(shí)體對(duì)象Student,用作數(shù)據(jù)傳輸?shù)妮d體,下面是Student.cs的內(nèi)容
- using System.Runtime.Serialization;
- namespace WCFRest
- {
- /// <summary>
- /// DataContract 數(shù)據(jù)契約:服務(wù)端和客戶端之間要傳送的自定義數(shù)據(jù)類型
- /// </summary>
- [DataContract]
- public class Student
- {
- /// <summary>
- /// 在數(shù)據(jù)傳送過(guò)程中,只有成員變量可以被傳送而成員方法不可以。
- /// 并且只有當(dāng)成員變量加上DataMember時(shí)才可以被序列進(jìn)行數(shù)據(jù)傳輸,
- /// 如果不加DataMember,客戶端將無(wú)法獲得該屬性的任何信息
- /// </summary>
- [DataMember]
- public int Id { get; set; }
- [DataMember]
- public string Name { get; set; }
- }
- }
同時(shí)我們創(chuàng)建一個(gè)類,用來(lái)模擬數(shù)據(jù)庫(kù)的存儲(chǔ)
- using System.Collections.Generic;
-
- namespace WCFRest
- {
- public class UserList
- {
- private static readonly UserList _Instance = new UserList();
- private UserList() { }
-
- public static UserList Instance
- {
- get { return _Instance; }
- }
-
- public IList<Student> Users
- {
- get { return _Users; }
- }
-
- private IList<Student> _Users = new List<Student>{
- new Student {Id = 1, Name = "張三" },
- new Student {Id = 2, Name = "李四" },
- new Student {Id = 3, Name = "王五" }
- };
- }
- }
3、創(chuàng)建服務(wù)契約
下面我們?cè)陧?xiàng)目添加一個(gè)WCF Service
首先修改IStudnetService接口,配置Rest的URL路徑
- using System.Collections.Generic;
- using System.ServiceModel;
- using System.ServiceModel.Web;
-
- namespace WCFRest
- {
- [ServiceContract]
- public interface IStudentService
- {
- [OperationContract]
- [WebInvoke(Method = "GET",
- RequestFormat = WebMessageFormat.Json,
- ResponseFormat = WebMessageFormat.Json,
- UriTemplate = "GetStudentById/Id={Id}"
- )]
- Student GetStudentById(string Id);
-
- [OperationContract]
- [WebInvoke(Method = "GET",
- RequestFormat = WebMessageFormat.Json,
- ResponseFormat = WebMessageFormat.Json,
- UriTemplate = "GetStudentList"
- )]
- IList<Student> GetStudentList();
- }
- }
4、修改StudentService類,實(shí)現(xiàn)Rest方法
- using System.Collections.Generic;
-
- namespace WCFRest
- {
- public class StudentService : IStudentService
- {
-
- public Student GetStudentById(string Id)
- {
- return StudentList.Instance.Users[int.Parse(Id)];
- }
-
- public IList<Student> GetStudentList()
- {
- return StudentList.Instance.Users;
- }
- }
- }
5、配置Service和Behavior
在Web.Config中配置我們的Rest服務(wù)
- <system.serviceModel>
- <services>
- <service name="WCFRest.StudentService" behaviorConfiguration="serviceBehavior">
- <endpoint address="" binding="webHttpBinding" contract="WCFRest.IStudentService"
- behaviorConfiguration="web"></endpoint>
- </service>
- </services>
- <behaviors>
- <serviceBehaviors>
- <behavior name="serviceBehavior">
- <serviceMetadata httpGetEnabled="true"/>
- <serviceDebug includeExceptionDetailInFaults="false"/>
- </behavior>
- <behavior>
- <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
- <serviceDebug includeExceptionDetailInFaults="false"/>
- </behavior>
- </serviceBehaviors>
- <endpointBehaviors>
- <behavior name="web">
- <webHttp/>
- </behavior>
- </endpointBehaviors>
- </behaviors>
- <protocolMapping>
- <add binding="basicHttpsBinding" scheme="https" />
- </protocolMapping>
- <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
- </system.serviceModel>
OK,下面測(cè)試一下我們的程序,右擊Student.svc文件,選擇View In Browser,我們將會(huì)看到下面的運(yùn)行結(jié)果
然后我們?cè)趗rl后面加上我們定義的Rest地址就可以訪問(wèn)Rest服務(wù)了
說(shuō)明:本文絕大部分內(nèi)容摘自下面文章
http://www.topwcftutorials.net/2013/09/simple-steps-for-restful-service.html
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。