我创建了一个非常基本的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)的缓存结果。
我找不出什么问题,请多多指教,谢谢。
也没有为我工作。让我知道你是否碰巧解决了这个问题。 –