1

I am using Nancy, OWIN and Microsoft.Owin.Host.HttpListener to self host REST service.

One of my services needs to return a lot of data and I want to stream it to the client.

This doesn't work:

Get["/stream"] = _ =>
{
    var response = new Response();
    response.Headers.Add("Transfer-Encoding", "Chunked");
    response.ContentType = "text/plain";
    response.Contents = s =>
    {
        byte[] bytes = Encoding.UTF8.GetBytes("Hello World\n");
        for (int i = 0; i < 10; ++i)
        {
            s.Write(bytes, 0, bytes.Length);
            s.Flush();
            Thread.Sleep(2000);
        }
    };

    return response;
};

Should this work and if so what am I doing wrong? Looking at the results in Fiddler, Content-Length is set and the entire response is only seen after 20 seconds.

(I can't find any conclusive answers on Google. How do I write streamed output in NancyFX? - this questions seems to imply it doesn't work but I wonder if it has been fixed as this was asked a while ago)

Community
  • 1
  • 1
Iain
  • 2,500
  • 1
  • 20
  • 24

1 Answers1

2

For completeness, this does work correctly but it doesn't work with Fiddler. Fiddler captures the entire response in one go.

Iain
  • 2,500
  • 1
  • 20
  • 24