加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 无法让AspNetCacheProfile在WCF 4.0服务中工作

发布时间:2020-12-15 21:35:19 所属栏目:百科 来源:网络整理
导读:我在.svc文件中创建了一个非常基本的WCF服务应用程序,其中包含以下代码: using System.Collections.Generic;using System.ServiceModel;using System.ServiceModel.Activation;using System.ServiceModel.Web;namespace NamesService{ [ServiceContract] [S
我在.svc文件中创建了一个非常基本的WCF服务应用程序,其中包含以下代码:

using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;

namespace NamesService
{
    [ServiceContract]
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class NamesService
    {
        List<string> Names = new List<string>();

        [OperationContract]
        [AspNetCacheProfile("CacheFor60Seconds")]
        [WebGet(UriTemplate="")]
        public List<string> GetAll()
        {
            return Names;
        }

        [OperationContract]
        public void Save(string name)
        {
            Names.Add(name);
        }
    }
 }

web.config看起来像这样:

<?xml version="1.0"?>
<configuration>
<system.web>
    <compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior>
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
</system.serviceModel>
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.web>
    <caching>
        <outputCache enableOutputCache="true"/>
        <outputCacheSettings>
            <outputCacheProfiles>
                <add name="CacheFor60Seconds" location="Server" duration="60" varyByParam="" />
            </outputCacheProfiles>
        </outputCacheSettings>
    </caching>
</system.web>

如您所见,GetAll方法已使用AspNetCacheProfile进行修饰,而cacheProfileName引用了web.config的“ChacheFor60Seconds”部分.

我在WCF测试客户端中运行以下序列:

1)使用参数“Fred”调用Save

2)致电GetAll – >正如预期的那样,“弗雷德”被归还.

3)使用参数“Bob”调用Save

4)调用GetAll – >这次“弗雷德”和“鲍勃”被退回.

我期待第二次调用GetAll时只返回“Fred”,因为它应该返回步骤(2)的缓存结果.

我无法弄清楚问题是什么,所以请一定帮助.

解决方法

您正在尝试缓存没有任何参数的完整结果,因此您的设置应该是

<outputCacheSettings>
    <outputCacheProfiles>
        <add name="CacheFor60Seconds" location="Server" duration="60"
        varyByParam="none" />
    </outputCacheProfiles>
 </outputCacheSettings>

编辑:

[OperationContract]
[AspNetCacheProfile("CacheFor60Seconds")]
[WebGet]
public List<string> GetAll()
{
    return Names;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读