2

我正在尝试通过 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

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

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

似乎自托管品种不缓冲它的响应?这是性能还是兼容性问题?

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

非常感谢。

4

1 回答 1

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();
    }
});
于 2013-04-12T15:07:41.150 回答