Stream Stream (SqlFile-) using Nancy

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

So far I have managed to transfer the stream, but Nancy StreamResponse copies sourcestream (= SqlFileStream) to the output stream, which leads to an increase in massive memory. Where I just wanted to send a stream through.

I did this job in WebApi, where WebApi was registered in the Owin pipeline. No increase in memory is noticeable, which is excellent when we talk about fairly large streams (> 100 MB). But of course, I would prefer to stick with one framework API, if possible.

Any tips?

+2
source share
1 answer

, . .

Nancy.Response = > FlushingStreamResponse. mimetype , 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;
    }
}
+1

All Articles