如何在NancyFX中写入流式输出? [英] How do I write streamed output in NancyFX?

查看:63
本文介绍了如何在NancyFX中写入流式输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Nancy编写一个简单的Web应用程序.至少有一个请求导致了长度未知的流,因此我无法提供Content-Length.我想使用Transfer-Encoding: chunked或(在这种情况下,也可以使用Connection: close).

I'm writing a simple web application using Nancy. At least one request results in a stream of unknown length, so I can't provide Content-Length. I'd like to use Transfer-Encoding: chunked, or (equally acceptable in this case, Connection: close).

我对Nancy源代码进行了快速修改,并添加了Response.BufferOutput,并添加了将HttpContext.Response.BufferOutput设置为false的代码.您可以在这里看到它:

I've had a quick hack on the Nancy source code, and I've add Response.BufferOutput, and code to set HttpContext.Response.BufferOutput to false. You can see that here:

public class HomeModule : NancyModule
{
    public HomeModule()
    {
        Get["/slow"] = _ => new SlowStreamResponse();
    }

    private class SlowStreamResponse : Response
    {
        public SlowStreamResponse()
        {
            ContentType = "text/plain";
            BufferOutput = false;
            Contents = s => {
                byte[] bytes = Encoding.UTF8.GetBytes("Hello World\n");
                for (int i = 0; i < 10; ++i)
                {
                    s.Write(bytes, 0, bytes.Length);
                    Thread.Sleep(500);
                }
            };
        }
    }

它似乎没有任何作用. 5秒钟后,响应立即全部显示.我已经测试了这个简单的基于WebRequest的客户端.

It doesn't seem to have any effect. The response turns up all at once, after 5 seconds. I've tested this a simple WebRequest-based client.

如何获取分块输出以在Nancy中工作?我正在使用ASP.NET托管,但是我对其他托管选项的答案很感兴趣.

How do I get chunked output to work in Nancy? I'm using the ASP.NET hosting, but I'd be interested in answers for the other hosting options.

如果我使用HttpListener编写一个简单的服务器,则可以将SendChunked设置为true,它会发送分块输出,我的简单客户端正确地以分块形式接收该输出.

If I write a simple server using HttpListener, I can set SendChunked to true, and it sends chunked output, which my simple client correctly receives in chunks.

推荐答案

在实验期间,我发现我需要以下配置.首先,按照 Nancy所述设置您的web.config文件. Wiki .值得注意的是,为了设置disableoutputbuffer值(这是我们想要的值),似乎您当前还需要指定一个引导程序.在程序集中创建一个从Nancy.Hosting.Aspnet.DefaultNancyAspNetBootstrapper继承的类并在配置文件中指定它似乎可以正常工作.

During my experimentation, I discovered that I needed the following configuration. First, set up your web.config file as documented in the Nancy Wiki. It is worth noting that in order to set the disableoutputbuffer value (which is what we want), it appears that you currently need to also specify a bootstrapper. Creating a class in your assembly that inherits from Nancy.Hosting.Aspnet.DefaultNancyAspNetBootstrapper and specifying it in the configuration file appears to work.

<configSections>
  <section name="nancyFx" type="Nancy.Hosting.Aspnet.NancyFxSection" />
</configSections>
<nancyFx>
  <bootstrapper assembly="YourAssembly" type="YourBootstrapper"/>
  <disableoutputbuffer value="true" />
</nancyFx>

此后,您不应设置Transfer-Encoding标头.而是,以下路由定义似乎可以将结果从我的IIS Express开发服务器正确流式传输到Chrome:

Afterwards, you should not set the Transfer-Encoding header. Rather, the following route definition appears to correctly stream the results from my IIS Express development server to Chrome:

Get["/chunked"] = _ =>
{
  var response = new Response();
  response.ContentType = "text/plain";
  response.Contents = s =>
  {
    byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Hello World ");
    for (int i = 0; i < 10; ++i)
    {
      for (var j = 0; j < 86; j++)
      {
        s.Write(bytes, 0, bytes.Length);
      }
      s.WriteByte(10);
      s.Flush();
      System.Threading.Thread.Sleep(500);
    }
  };

  return response;
};

由于在其他

I specified more content per chunk than the previous example due to the minimum sizes prior to first render documented in other StackOverflow questions

这篇关于如何在NancyFX中写入流式输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆