我正在尝试使用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
似乎自托管的多样性并没有缓冲它的反应?这是性能还是兼容性问题?
如何在使用自托管方法时启用缓冲?
非常感谢。
答案 0 :(得分:5)
如何在使用自托管方式时启用缓冲?
您可以创建如下的ResponseFilter。我会说这有点激进,它会阻止其他ResponseFilters运行。您可以将其转换为Filter Attribute并仅在响应具有明显的性能优势时使用它。否则,我只是让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();
}
});