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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值