ServiceStack 流壓縮 (ServiceStack Stream Compression)


問題描述

ServiceStack 流壓縮 (ServiceStack Stream Compression)

I am returning a stream of data from a ServiceStack service as follows. Note that I need to do it this way instead of the ways outlined here because I need to perform some cleanup after the data has been written to the output stream.

using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
    fs.WriteTo(Response.OutputStream);
}
Response.EndRequest();
...cleanup code...

Compression is handled in the other services that return simple DTOs by using a ServiceRunner similar to this answer. However the stream response above never hits that code as the response object in OnAfterExecute is always null. I am able to manually compress the result inside of the service method as follows, but it requires a lot of setup to determine if and what compression is needed and manually setting up the correct HTTP headers (omitted below).

var outStream = new MemoryStream();
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using (var tinyStream = new GZipStream(outStream, CompressionMode.Compress))
{
    fs.CopyTo(tinyStream);
    outStream.WriteTo(Response.OutputStream);
}
Response.EndRequest();
...cleanup code...

Is there a way in ServiceStack to handle this compression for me similar to the way it works with the ServiceRunner?


參考解法

方法 1:

I'm not exactly sure what you like about the way ServiceStack handles compression within ServiceRunner. Is it because it is global across ServiceStack APIs?

For your example, I think something like below works and meets your need to perform some cleanup after the data has been written to the output stream ...

public class FStreamService : Service
{
    public object Get(FStream request)
    {
        var filePath = @"c:\test.xml";
        var compressFileResult = new CompressedFileResult(filePath); //CompressedResult in ServiceStack.Common.Web.CompressedFileResult
        compressFileResult.WriteTo(Response.OutputStream);
        Response.EndRequest();
        //  ...cleanup code...
    }
}

Based on comments update of above to add compression using some ServiceStack extensions

public object Get(FStream request)
{
    var filePath = @"c:\test.xml";
    using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    {
        var compressedBtyes = fs.ToUtf8String().Compress(this.RequestContext.CompressionType);
        new CompressedResult(compressedBtyes).WriteTo(Response.OutputStream);           
    }
    Response.EndRequest();
        //  ...cleanup code...
}

If you want it within a ServiceRunner something like this should work...

public override object OnAfterExecute(IRequestContext requestContext, object response)
{
    var resp = requestContext.Get<IHttpResponse>();
    response = requestContext.ToOptimizedResult(requestContext.Get<IHttpResponse>().OutputStream);       

    return base.OnAfterExecute(requestContext, response);
}

(by bpruitt‑goddardpaaschpa)

參考文件

  1. ServiceStack Stream Compression (CC BY‑SA 3.0/4.0)

#compression #servicestack #C#






相關問題

CSS 壓縮和組合 / js 縮小 - 在運行時或構建時更好? (CSS compression & combining / js minification - Better to do at runtime or at build time?)

在javascript中壓縮包含音頻PCM數據的blob (compress blob containing audio PCM data in javascript)

如何提高@font-face 的加載性能 (How to improve loading performance of @font-face)

ServiceStack 流壓縮 (ServiceStack Stream Compression)

有沒有辦法讀取壓縮存檔的屬性? (Is there any way to read the properties of a Compressed Archive?)

德爾福 2009 中的 Zlib (Zlib in Delphi 2009)

SQL 2008開啟頁面壓縮後如何回收空間? (How to reclaim space after turning on page compression in SQL 2008?)

本機 Lua 中的高效可變字節數組 (Efficient mutable byte array in native Lua)

在 JavaScript 中壓縮純文本? (Compressing plaintext in JavaScript?)

如何遞歸壓縮文件夾? (How to zip a folder recursively?)

使用 GZIPOutputStream 壓縮字符串 (Compressing a string using GZIPOutputStream)

壓縮後 1 KB 可以容納多少文本? (How much text I can fit in 1 kilobyte with compression?)







留言討論