带有流的WCF.NET 4 OutputCaching似乎不起作用

带有流的WCF.NET 4 OutputCaching似乎不起作用,wcf,.net-4.0,streaming,authorization,outputcache,Wcf,.net 4.0,Streaming,Authorization,Outputcache,我在.NET4-IIS7上通过WCF REST服务进行输出缓存时遇到问题。通过实现ServiceAuthorizationManager,我的服务有一个自定义的授权方案,每个请求都应该有一个授权方案,任何缓存都必须在请求授权后进行。到目前为止,这似乎是可行的,只有缓存部分我看不到发生 我有以下经营合同: [OperationContract] [AspNetCacheProfile("PageZIP")] [WebGet(UriTemplate = "pages/{page_id}")] Sys

我在.NET4-IIS7上通过WCF REST服务进行输出缓存时遇到问题。通过实现ServiceAuthorizationManager,我的服务有一个自定义的授权方案,每个请求都应该有一个授权方案,任何缓存都必须在请求授权后进行。到目前为止,这似乎是可行的,只有缓存部分我看不到发生

我有以下经营合同:

[OperationContract]
[AspNetCacheProfile("PageZIP")]
[WebGet(UriTemplate = "pages/{page_id}")]
System.IO.Stream getPage(string page_id);
以及以下web.config文件:

<system.web>
<compilation targetFramework="4.0" />
<customErrors mode="Off" />
<authentication mode="None" />
<caching>
    <outputCache enableOutputCache="true"/>
    <outputCacheSettings>
        <outputCacheProfiles>
            <add name="PageZIP" duration="7200" location="ServerAndClient"
                     varyByParam="page_id" />
        </outputCacheProfiles>
    </outputCacheSettings>
</caching>
</system.web>
    <system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior>
                <serviceAuthorization serviceAuthorizationManagerType="api.Authorization, api" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
        <webHttpEndpoint>
            <standardEndpoint transferMode="StreamedResponse" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json" />
        </webHttpEndpoint>
    </standardEndpoints></system.serviceModel>
如果正在缓存输出,则根本不应执行此方法。。。我希望流被缓存并直接提供服务,而不需要查询数据库和磁盘读取。每个zipfile的大小约为1MB。
我遗漏了什么或做错了什么?

在使用设置一段时间后,答案出来了:如果transferMode是流式传输,OutputCache将无法工作,即使您实现了自己的OutputCacheProvider。这背后的原因是,在流式响应场景中,您告诉WCF不要将响应缓冲在内存中,并尝试从任何位置读取响应,然后将其发送到传输级别。OutputCache依赖于对象在返回之前完全在内存中,因此WCF可以保留对它的引用并将其放在缓存中。 在您的场景中,您可以看到启用不带输出缓存的流是否比读取并将对象保留在内存中更快,这样您就可以直接输出它

public System.IO.Stream getPage(string page_id) {
    File zip = FileMapper.getZip(pid);
    ctx.OutgoingResponse.Headers.Clear();
    ctx.OutgoingResponse.Headers.Add("Content-Disposition", "attachment; filename=" + page_id + ".zip");
    ctx.OutgoingResponse.Headers.Add("Content-Length", zip.size.ToString());
    ctx.OutgoingResponse.ContentType = "application/zip";

    Log.write("OpenRead", LogType.Info);
    return System.IO.File.OpenRead(zip.path);
}