servicestack self-hosted service uses chunked enco

2019-05-23 02:52发布

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条回答
手持菜刀,她持情操
2楼-- · 2019-05-23 03:30

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();
    }
});
查看更多
登录 后发表回答