【问题标题】:Unable to get AspNetCacheProfile to work in a WCF 4.0 service无法让 AspNetCacheProfile 在 WCF 4.0 服务中工作
【发布时间】:2012-06-03 19:49:50
【问题描述】:

我在 .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 -> 按预期返回“Fred”。

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

4) 调用 GetAll -> 这次返回“Fred”和“Bob”。

我希望在第二次调用 GetAll 时只返回“Fred”,因为它应该返回步骤 (2) 中的缓存结果。

我无法弄清楚问题是什么,所以请提供一些帮助。

【问题讨论】:

  • 对我也不起作用。如果你碰巧解决了这个问题,请告诉我。

标签: c# wcf c#-4.0 caching


【解决方案1】:

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

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

编辑:

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

【讨论】:

  • 谢谢,但它仍然无法正常工作。我想知道是不是因为我正在使用 WCF 测试客户端来测试这个?缓存是否适用于 WCF 测试客户端?
  • 不,它应该以任何方式工作..你可以试试 [OperationContract] [AspNetCacheProfile("CacheFor60Seconds")] [WebGet] public List GetAll() { return Names; }
  • 谢谢。我将 [WebGet(UriTemplate="")] 更改为 [WebGet] 但不幸的是它仍然无法正常工作。
【解决方案2】:

您的web.config 配置文件有一个双&lt;system.web&gt; 部分,请尝试合并它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多