问题
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:
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
Same basic service, self-hosting (command line):
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?
Many thanks.
回答1:
How can I turn on buffering when using the self-hosting method?
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();
}
});
来源:https://stackoverflow.com/questions/15928202/servicestack-self-hosted-service-uses-chunked-encoding-is-unbuffered