.net 使用WebInvoke的HTTP缓存WCF REST/SOAP操作

.net 使用WebInvoke的HTTP缓存WCF REST/SOAP操作,.net,wcf,rest,soap,outputcache,.net,Wcf,Rest,Soap,Outputcache,我有一个WCF Web服务(不是Web Api),看起来像这样: public interface ILogsService { [OperationContract] [WebInvoke(UriTemplate = "/Find")] [Description("-Service description-")] FindLogsResult FindLogs(LogsFilterRequest request); } 我想知道是否有一种方法(或更好的方法)通

我有一个WCF Web服务(不是Web Api),看起来像这样:

public interface ILogsService
{
    [OperationContract]
    [WebInvoke(UriTemplate = "/Find")]
    [Description("-Service description-")]
    FindLogsResult FindLogs(LogsFilterRequest request);
}
我想知道是否有一种方法(或更好的方法)通过将HTTP缓存头发送回客户端来缓存服务响应

我已经测试并看到,这可以通过使用:

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddSeconds(30));
但如果可能的话,我想要一个“更清洁”的解决方案


此服务具有aspNetCompatibilityEnabled=“true”,并且将始终托管在IIS中。

您应该能够向方法添加缓存属性:

[AspNetCacheProfile("CacheMediumTerm")]
public FindLogsResult FindLogs(LogsFilterRequest request) {
    //...
然后可以在配置文件中配置:

<!-- ...  -->
<system.web>
   <caching>
      <outputCacheSettings>
         <outputCacheProfiles>
            <add name="CacheMediumTerm" duration="120" varyByParam="none" sqlDependency="MyTestDatabase:MyTable"/>
         </outputCacheProfiles>
      </outputCacheSettings>
   </caching>
   <!-- ... -->
</system.web>


我试过了,但它不允许我使用,因为我使用的是
WebInvoke
(不是
WebGet
)啊-抱歉,它不能与WebInvoke一起使用:(-可能是手动标题!