如何base.RequestContext.ToOptimizedResultUsingCache()返回Accept-Encoding的字符串:deflate?

时间:2013-09-16 03:56:42

标签: servicestack

我有以下代码...... 和接受编码:deflate

public object Get(DTOs.Product request)
 {
    ...


    var sCache = base.RequestContext.ToOptimizedResultUsingCache(
    this.CacheClient, cacheKey, expireInTimespan, () =>
    {            
        // Business layer returns resultant dataset as XmlDocument
        ...

        return sXML.InnerXml;
    });

    //returns ServiceStack.Common.Web.HttpResult;
    return GenerateResp(sCache, base.Request.Headers["Accept"]);            
 }

问题是 base.RequestContext.ToOptimizedResultUsingCache 返回 ServiceStack.Common.Web.CompressedResult ,即使我正在返回XML。我了解 Accept-Encoding:deflate 会导致 RequestContext.CompressionType deflate

当没有Accept-Encoding(通过fiddler测试)时,上面的代码工作正常。

但是,如果请求来自浏览器,它将作为Compressed,在这种情况下如何将sCache作为字符串传递给GenerateResp?

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

我可以通过修改代码来解决问题......

public object Get(DTOs.Product request)
{
    ...

    var objCache = base.RequestContext.ToOptimizedResultUsingCache(
                      this.CacheClient, cacheKey, expireInTimespan, () =>
    {            
        // Business layer returns resultant dataset as XmlDocument
        ...

        return sXML.InnerXml;
    });

    string compressionType = base.RequestContext.CompressionType;
    bool doCompression = compressionType != null;
    string transformed = "";

    if (doCompression)
    {
        byte[] bCache = ((ServiceStack.Common.Web.CompressedResult)(objCache)).Contents;
        transformed = bCache.Decompress(base.RequestContext.CompressionType);
    }
    else
    {
        transformed = (string)objCache;
    }

    //returns ServiceStack.Common.Web.HttpResult;
    //In GenerateResp, If compressionType contains "gzip" or "deflate", I compress back the response to respective compression, and add respective Content-Encoding to the header.
    return GenerateResp(transformed, base.Request.Headers["Accept"], compressionType);            
}

谢谢。