国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
簡(jiǎn)單五步創(chuàng)建一個(gè)WCF Rest Service

1、創(chuàng)建一個(gè)WCF Service Application



2、創(chuàng)建一個(gè)實(shí)體對(duì)象Student,用作數(shù)據(jù)傳輸?shù)妮d體,下面是Student.cs的內(nèi)容

  1. using System.Runtime.Serialization;  
  2. namespace WCFRest  
  3. {  
  4.     /// <summary>  
  5.     /// DataContract 數(shù)據(jù)契約:服務(wù)端和客戶端之間要傳送的自定義數(shù)據(jù)類型  
  6.     /// </summary>  
  7.     [DataContract]  
  8.     public class Student  
  9.     {  
  10.         /// <summary>  
  11.         /// 在數(shù)據(jù)傳送過(guò)程中,只有成員變量可以被傳送而成員方法不可以。  
  12.         /// 并且只有當(dāng)成員變量加上DataMember時(shí)才可以被序列進(jìn)行數(shù)據(jù)傳輸,  
  13.         /// 如果不加DataMember,客戶端將無(wú)法獲得該屬性的任何信息  
  14.         /// </summary>  
  15.         [DataMember]  
  16.         public int Id { get; set; }  
  17.         [DataMember]  
  18.         public string Name { get; set; }  
  19.     }  
  20. }  

同時(shí)我們創(chuàng)建一個(gè)類,用來(lái)模擬數(shù)據(jù)庫(kù)的存儲(chǔ)

  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、創(chuàng)建服務(wù)契約

下面我們?cè)陧?xiàng)目添加一個(gè)WCF Service


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

  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類,實(shí)現(xiàn)Rest方法

  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服務(wù)

  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,下面測(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)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
vs2013下的WCFRest 模板開發(fā)WCF
ServiceHost與宿主寄存
WCF之旅(9):如何在WCF中使用tcpTrace來(lái)進(jìn)行Soap Trace
WCF配置文件全攻略
WCF配置文件 - 落霞與孤騖齊飛,秋水共長(zhǎng)天一色
如何:?jiǎn)⒂?WCF 身份驗(yàn)證服務(wù)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服