[英]HTTP caching WCF REST/SOAP action using WebInvoke
I have a WCF Web Service (not Web Api) that looks like this:我有一个 WCF Web 服务(不是 Web Api),如下所示:
public interface ILogsService
{
[OperationContract]
[WebInvoke(UriTemplate = "/Find")]
[Description("-Service description-")]
FindLogsResult FindLogs(LogsFilterRequest request);
}
I would like to know if there's a way (or a better way) to cache the service response by sending HTTP cache headers back to the client.我想知道是否有一种方法(或更好的方法)通过将 HTTP 缓存标头发送回客户端来缓存服务响应。
I've tested and seen that this can be accomplished by using:我已经测试并看到这可以通过使用来完成:
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddSeconds(30));
But I would like a "cleaner" solution if possible.但如果可能的话,我想要一个“更清洁”的解决方案。
This service has aspNetCompatibilityEnabled="true"
and will always be hosted in IIS.此服务具有aspNetCompatibilityEnabled="true"
,并将始终托管在 IIS。
You should be able to add a cache attribute to your method:您应该能够向您的方法添加缓存属性:
[AspNetCacheProfile("CacheMediumTerm")]
public FindLogsResult FindLogs(LogsFilterRequest request) {
//...
Which you can then configure in your config file:然后你可以在你的配置文件中配置:
<!-- ... -->
<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="CacheMediumTerm" duration="120" varyByParam="none" sqlDependency="MyTestDatabase:MyTable"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
<!-- ... -->
</system.web>
More information on this feature is available here:有关此功能的更多信息,请参见此处:
http://msdn.microsoft.com/en-us/library/ee230443.aspx http://msdn.microsoft.com/en-us/library/ee230443.aspx
Just to close this question, the only way I could get this working was via HTTP Context:只是为了结束这个问题,我能让这个工作的唯一方法是通过 HTTP 上下文:
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddSeconds(30));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.