打开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);
}
}

然后在建立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);
}
}

然后在配置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="" varyByParam="format"/>
<add name="CacheFor30Senconds" duration="" 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内结果都是下图,超过时间后就是另一个结果了

最新文章

  1. C# 动态修改Config
  2. 清北学堂模拟赛day7 错排问题
  3. Python 文件读写,条件循环(三次登录锁定账号实例)
  4. Minimum Adjustment Cost
  5. How to crack gbooks
  6. Windows Server 2008 R2 备份和恢复 (转)
  7. 草根玩微博 中产玩微信 土豪玩什么?支持Yo的iWatch?
  8. Android 蓝牙 BLE 开发笔记
  9. Android Gradle 编译错误Java finished with non-zero exit value 2
  10. ByteArray
  11. android自定义控件实现TextView按下后字体颜色改变
  12. C++高精度运算类bign (重载操作符)
  13. 实战nginx 基础知识总结(一)1.1
  14. 第六百二十六天 how cna I 坚持
  15. a标签href无值 onclick事件跳转
  16. selenium 定位元素成功, 但是输入失败 (textarea)
  17. MySQL状态变量Aborted_connects与Aborted_clients浅析
  18. Node.js模板引擎的深入探讨
  19. canvas绘图基础
  20. skipper http router 工具

热门文章

  1. Entity Framework Tutorial Basics(33):Spatial Data type support in Entity Framework 5.0
  2. CodeForces - 710C Magic Odd Square(奇数和幻方构造)
  3. 关于C# WinForm_Tree View的一些基本用法(摘抄)
  4. Repeater控件最后一笔记录高亮显示
  5. 企业sudo权限规划详解 (实测一个堆命令搞定)
  6. Django会话,用户和注册之cookie
  7. duddo在xml里面出现红叉的解决方法
  8. 通知类型 重点: 环绕通知 (XML配置)
  9. 树形DP--求树上任意两点间距离和
  10. [转]10分钟梳理MySQL知识点:揭秘亿级高并发数据库调优与最佳实践法则