C# 无法使AspNetCacheProfile在WCF 4.0服务中工作
我在.svc文件中使用以下代码创建了一个非常基本的WCF服务应用程序:C# 无法使AspNetCacheProfile在WCF 4.0服务中工作,c#,wcf,c#-4.0,caching,C#,Wcf,C# 4.0,Caching,我在.svc文件中使用以下代码创建了一个非常基本的WCF服务应用程序: using System.Collections.Generic; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Web; namespace NamesService { [ServiceContract] [ServiceBehavior(InstanceContext
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);
}
}
}
使用System.Collections.Generic;
使用System.ServiceModel;
使用System.ServiceModel.Activation;
使用System.ServiceModel.Web;
名称空间服务
{
[服务合同]
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
公共类名称服务
{
列表名称=新列表();
[经营合同]
[AspNetCacheProfile(“缓存60秒”)]
[WebGet(UriTemplate=”“)]
公共列表GetAll()
{
返回姓名;
}
[经营合同]
公共作废保存(字符串名称)
{
名称。添加(名称);
}
}
}
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) Call GetAll->返回“Fred”,与预期一样
3) 使用参数“Bob”调用Save
4) 调用GetAll->这次返回“Fred”和“Bob”
我希望在第二次调用GetAll时只返回“Fred”,因为它应该返回步骤(2)中缓存的结果
我不知道问题出在哪里,因此如果您能提供帮助,我将不胜感激。您正在尝试缓存没有任何参数的完整结果,因此您的设置应该是
<outputCacheSettings>
<outputCacheProfiles>
<add name="CacheFor60Seconds" location="Server" duration="60"
varyByParam="none" />
</outputCacheProfiles>
</outputCacheSettings>
编辑:
[OperationContract]
[AspNetCacheProfile("CacheFor60Seconds")]
[WebGet]
public List<string> GetAll()
{
return Names;
}
[运营合同]
[AspNetCacheProfile(“缓存60秒”)]
[WebGet]
公共列表GetAll()
{
返回姓名;
}
您的web.config
配置文件有一个双
部分,请尝试合并它们。谢谢,但它仍然不起作用。我想知道这是否是因为我正在使用WCF测试客户端来测试它?缓存是否与WCF测试客户端一起工作?不,它应该以任何方式工作..您可以尝试[OperationContract][AspNetCacheProfile(“CacheFor60Seconds”)][WebGet]公共列表GetAll(){返回名称;}谢谢。我将[WebGet(UriTemplate=“”)更改为[WebGet],但不幸的是,它仍然不起作用。对我来说也不起作用。如果你能解决这个问题,请告诉我。