c# – 无法让AspNetCacheProfile在WCF 4.0服务中工作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 无法让AspNetCacheProfile在WCF 4.0服务中工作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2228字,纯文字阅读大概需要4分钟。
内容图文
我在.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;
}
内容总结
以上是互联网集市为您收集整理的c# – 无法让AspNetCacheProfile在WCF 4.0服务中工作全部内容,希望文章能够帮你解决c# – 无法让AspNetCacheProfile在WCF 4.0服务中工作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。