2012-06-03 21 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>部分,嘗試合併他們。