ASP.. NET Web API控制器输出总是被缓冲

本文关键字:缓冲 输出 控制器 NET Web API ASP | 更新日期: 2023-09-27 18:03:14

如何发送没有缓冲的输出?我是这样定义我的API控制器的:

public class DefaultController : ApiController
{
    [HttpGet]
    [Route]
    public HttpResponseMessage Get()
    {
        var response = Request.CreateResponse();
        response.Content = new PushStreamContent(
            (output, content, context) =>
            {
                using (var writer = new StreamWriter(output))
                {
                    for (int i = 0; i < 5; i++)
                    {
                        writer.WriteLine("Eh?");
                        writer.Flush();
                        Thread.Sleep(2000);
                    }
                }
            },
            "text/plain");
        return response;
    }
}

输出一次出现在浏览器中,所以看起来它等待开始发送直到完成。我定义了这个属性:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
class NoBufferAttribute : Attribute, IControllerConfiguration
{
    public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
    {
        controllerSettings.Services.Replace(
            typeof(IHostBufferPolicySelector),
            new BufferPolicy());
    }
    class BufferPolicy : IHostBufferPolicySelector
    {
        public bool UseBufferedInputStream(object hostContext)
        {
            return false;
        }
        public bool UseBufferedOutputStream(HttpResponseMessage response)
        {
            return false;
        }
    }
}

并应用于控制器:

    [NoBuffer]
    public class DefaultController : ApiController
    {
          ...
    }

它没有帮助。所有输出将同时出现在浏览器中。

看起来问题是关于冲洗的。我将代码改为如下:

        var response = Request.CreateResponse();
        response.Content = new PushStreamContent(
            (output, content, context) =>
            {
                using (var writer = new StreamWriter(output))                    
                {
                    var s = Stopwatch.StartNew();
                    while (s.Elapsed < TimeSpan.FromSeconds(10))
                    {
                        writer.WriteLine(s.Elapsed);
                        writer.Flush();                            
                    }
                }
            },
            "text/plain");

现在我可以看到正在进行的输出。禁用gzip编码并不能帮助查看更小块的内容。

ASP.. NET Web API控制器输出总是被缓冲

请尝试'text/event-stream'作为PushStreamContent构造函数的mediaType参数:

public class DefaultController : ApiController
{
    [HttpGet]
    [Route]
    public HttpResponseMessage Get()
    {
        var response = Request.CreateResponse();
        response.Content = new PushStreamContent(
            (output, content, context) =>
            {
                using (var writer = new StreamWriter(output))
                {
                    for (int i = 0; i < 5; i++)
                    {
                        writer.WriteLine("Eh?");
                        writer.Flush();
                        Thread.Sleep(2000);
                    }
                }
            },
            "text/event-stream");
        return response;
    }
}