我在.svc文件中创建了一个非常基本的WCF服务应用程序,其中包含以下代码:
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)使用参数“Fred”调用Save
2)致电GetAll – >正如预期的那样,“弗雷德”被归还.
3)使用参数“Bob”调用Save
4)调用GetAll – >这次“弗雷德”和“鲍勃”被退回.
我期待第二次调用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; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。