2012-06-03 20 views
4

我创建了一个非常基本的WCF服务应用程序与.svc文件下面的代码工作:无法获得AspNetCacheProfile在一个WCF 4.0服务

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)与参数 “弗雷德” 呼叫保存

2)呼叫GETALL - > “弗雷德” 返回,符合市场预期。

3)与参数 “鲍勃” 呼叫保存

4)呼叫GETALL - >这个时候 “弗雷德” 和 “鲍勃” 返回。

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

我找不出什么问题,请多多指教,谢谢。

+0

也没有为我工作。让我知道你是否碰巧解决了这个问题。 –

回答

1

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

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

编辑:

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

谢谢,但它仍然无法正常工作。我不知道是否因为我使用WCF测试客户端来测试它?缓存是否与WCF测试客户端一起工作? – isxpjm

+0

没有它应该工作anywhich ways..can尝试[OperationContract的] [AspNetCacheProfile( “CacheFor60Seconds”) [WebGet] 公开名单 GETALL(){ 回报 名称; } –

+0

谢谢。我将[WebGet(UriTemplate =“”)]更改为[WebGet],但仍然无法正常工作。 – isxpjm

0

web.config配置文件有双重<system.web>部分,尝试合并他们。