servicestack自托管服务使用分块编码 - 是否是无缓冲的?

Jor*_*ris 2 .net performance web-services self-hosting servicestack

我正在尝试使用hello world示例和自托管示例来学习ServiceStack.我正在请求JSON内容.

我在响应标头中注意到以下内容:

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

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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

看来自托管的多样性并没有缓冲它的反应?这是性能还是兼容性问题?

如何在使用自托管方法时打开缓冲?

非常感谢.

paa*_*hpa 5

如何在使用自托管方法时打开缓冲?

您可以创建一个ResponseFilter,如下所示.我会说这有点激进,它会阻止其他ResponseFilters运行.您可以将其转换为过滤器属性,并仅在响应具有明显的性能优势时使用它.否则,我只是让AppHost处理响应.

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();
    }
});
Run Code Online (Sandbox Code Playgroud)