日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WCF rest 的帮助页面和缓存机制

發布時間:2023/11/30 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WCF rest 的帮助页面和缓存机制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

打開vs2010 在工具選項中選擇擴展管理器——聯機庫 安裝WCF REST Service Template 40(CS)模板

安裝后新建wcf服務應用程序,刪除默認建立的文件。

新建RestWcf4HelpPage.svc,代碼如下

using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Web; using System.Text;namespace RestWcf {[ServiceBehavior(IncludeExceptionDetailInFaults=false)][AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]public class RestWcf4HelpPage : IRestWcf4HelpPage{#region IRestWcf4HelpPage 成員[WebGet(UriTemplate="/Rest/Get/{id}")]public string GetData(string id){return "Hello Rest GetData";}[WebInvoke(UriTemplate="/Rest/Add/{id}",Method="POST")]public string AddData(string id){return "Hello Rest AddData";}[WebInvoke(UriTemplate = "/Rest/Update/{id}", Method = "PUT")]public string UpdateData(string id){return "Hello Rest UpdateData";}[WebInvoke(UriTemplate = "/Rest/Delete/{id}", Method = "Delete")]public string DeleteData(string id){return "Hello Rest DeleteData";}#endregion}[XmlSerializerFormat][ServiceContract]interface IRestWcf4HelpPage{[OperationContract]string GetData(string id);[OperationContract]string AddData(string id);[OperationContract]string UpdateData(string id);[OperationContract]string DeleteData(string id);} } View Code

然后在建立RestWcf4Cache.svc,代碼如下

using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Web; using System.Text;namespace RestWcf {[ServiceBehavior(IncludeExceptionDetailInFaults=false)][AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]public class RestWcf4Cache:IRestWcf4Cache{#region IRestWcf4Cache 成員[AspNetCacheProfile("CacheFor10Senconds")][WebGet(UriTemplate = "/Rest/Get/{id}")]public string GetData(string id){return "Hello Rest GetData " + DateTime.Now.ToString();}#endregion}[ServiceContract]interface IRestWcf4Cache{[OperationContract]string GetData(string id);} } View Code

然后在配置web.config文件,其中duration值緩存的有效時間。

<?xml version="1.0" encoding="utf-8"?> <configuration><system.web><compilation debug="true" targetFramework="4.0" /><caching><outputCacheSettings><outputCacheProfiles ><add name="CacheFor10Senconds" duration="10" varyByParam="format"/><add name="CacheFor30Senconds" duration="30" varyByParam="format"/></outputCacheProfiles></outputCacheSettings></caching></system.web><system.serviceModel><services><service name="RestWcf.RestWcf4HelpPage"><endpoint address="" behaviorConfiguration="RestWcf4HelpPageBehavior"binding="webHttpBinding" contract="RestWcf.IRestWcf4HelpPage" /></service><service name="RestWcf.RestWcf4Cache"><endpoint address="" behaviorConfiguration="RestWcf4CacheBehavior"binding="webHttpBinding" contract="RestWcf.IRestWcf4Cache" /></service></services><behaviors><endpointBehaviors><behavior name="RestWcf4HelpPageBehavior"><webHttp helpEnabled="true" /></behavior><behavior name="RestWcf4CacheBehavior"><webHttp helpEnabled="true" /></behavior></endpointBehaviors><serviceBehaviors><behavior name=""><serviceMetadata httpGetEnabled="true" /><serviceDebug includeExceptionDetailInFaults="false" /></behavior></serviceBehaviors></behaviors><serviceHostingEnvironment aspNetCompatibilityEnabled="true"multipleSiteBindingsEnabled="true" /></system.serviceModel><system.webServer><modules runAllManagedModulesForAllRequests="true"/></system.webServer></configuration>

?幫助頁面的效果如下圖

get請求結果如下圖

緩存效果如圖,緩存的時間設置為10s,在10s內結果都是下圖,超過時間后就是另一個結果了

轉載于:https://www.cnblogs.com/ZJ199012/p/4078890.html

總結

以上是生活随笔為你收集整理的WCF rest 的帮助页面和缓存机制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。