servicestack自托管服务使用分块编码 - 是无缓冲? [英] servicestack self-hosted service uses chunked encoding - is unbuffered?

查看:140
本文介绍了servicestack自托管服务使用分块编码 - 是无缓冲?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我努力学习ServiceStack与问候世界的例子,并自托管的例子。我提出请求JSON内容。

I am trying to learn ServiceStack with the hello world examples and self-hosted example. I am making requests for JSON content.

我注意到下面的响应头:

I have noticed the following in the response headers:

托管在ASP.Net项目的基本服务:

Basic service hosted in an ASP.Net project:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Wed, 10 Apr 2013 12:49:46 GMT
X-AspNet-Version: 4.0.30319
X-Powered-By: ServiceStack/3.943 Win32NT/.NET
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Length: 16   <-------------------------------------
Connection: Close

相同的基本服务,自托管(命令行):

HTTP/1.1 200 OK
Transfer-Encoding: chunked <-------------------------------
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
X-Powered-By: ServiceStack/3.943 Win32NT/.NET
Date: Wed, 10 Apr 2013 12:48:50 GMT

看来自托管各种不缓冲它的反应​​?这是一个性能和兼容性问题?

It seems the self-hosted variety does not buffer it's responses? Is this a performance or compatibility concern?

我如何可以打开缓冲使用自托管的方法是什么时候?

How can I turn on buffering when using the self-hosting method?

非常感谢。

推荐答案

我如何可以打开缓冲使用自托管的方法是什么时候?

您可以创建一个ResponseFilter如下图所示。我会说这是一种积极的,它会$运行p $ pvent其他ResponseFilters。你可以把它变成一个筛选属性,并只使用它时,有一个明确的为响应性能优势。否则,我只想让APPHOST处理响应。

You could create a ResponseFilter like below. I would say this is kind of aggressive and it would prevent other ResponseFilters from running. You could turn it into a Filter Attribute and only use it when there is a clear performance benefit for the Response. Otherwise, I would just let the AppHost handle the Response.

ResponseFilters.Add((httpReq, httpRes, dto) =>
{
    using (var ms = new MemoryStream())
    {
        EndpointHost.ContentTypeFilter.SerializeToStream(
            new SerializationContext(httpReq.ResponseContentType), dto, ms);

        var bytes = ms.ToArray();

        var listenerResponse = (HttpListenerResponse)httpRes.OriginalResponse;
        listenerResponse.SendChunked = false;
        listenerResponse.ContentLength64 = bytes.Length;
        listenerResponse.OutputStream.Write(bytes, 0, bytes.Length);
        httpRes.EndServiceStackRequest();
    }
});

这篇关于servicestack自托管服务使用分块编码 - 是无缓冲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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