Stream an (SqlFile-)Stream using Nancy

假装没事ソ 提交于 2019-12-08 03:08:28

问题


I was wondering how to send an (in my case) SqlFileStream directly to the client through our Nancy-API without loading the stream in memory.

So far I succeeded in passing the stream, but Nancy's StreamResponse copies the sourcestream (=SqlFileStream) to the outputstream which causes a massive memory increase. Where I would just like it to send the stream through.

I made this work in WebApi where WebApi was registered in the Owin-pipeline. No memory increase is noticeable, which is great when we are talking about pretty big streams (>100MB). But of course I'd rather stick to one API-application-framework if possible.

Any tips?


回答1:


I think I found a solution. It wasn't too difficult to do in the end.

I created a custom Nancy.Response => FlushingStreamResponse. Passing it a stream and a mimetype, results in immediate streaming to the client when this is the result of our GET.

public class FlushingStreamResponse : Response
{
    public FlushingStreamResponse(Stream sourceStream, string mimeType)
    {
        Contents = (stream) =>
        {
            var buffer = new byte[16 * 1024];
            int read;
            while ((read = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                stream.Write(buffer, 0, read);
                stream.Flush();
            }
            sourceStream.Dispose();
        };

        StatusCode = HttpStatusCode.OK;
        ContentType = mimeType;
    }
}


来源:https://stackoverflow.com/questions/29953301/stream-an-sqlfile-stream-using-nancy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!