我創建了一個非常基本的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)的緩存結果。
我找不出什麼問題,請多多指教,謝謝。
也沒有爲我工作。讓我知道你是否碰巧解決了這個問題。 –