Waiting time on a remote server for a json response with HttpListener

72 views
Skip to first unread message

Jose LM

unread,
Apr 23, 2014, 4:04:33 AM4/23/14
to servic...@googlegroups.com
Hi,

I'm using ServiceStack with a HttpListerner host and I notice that when I request a route with json content type to a remote host the response waiting time increases an average of 200ms, the same request at localhost the waiting time is near 0. The problem is when serialize to a stream in "JsonDataContractSerializer.Instance.SerializeToStream" the stream is the OutputStream of the HttpListenerResponse and the serializer is writing  property to property in this stream. Is like the problem of the StaticFileHandler that wrote in chunks of 4Kb and the download was too slow. I changed "ContentTypes.GetStreamSerializer"only for testing and write the response to a memory stream and the waiting time on a remote server is nearly 0 too. I don't know if this happens in other hosts else httplistener. I think that a solution can be modify ListenerResponse to use a MemoryStream and in the Close method write to OutputStream




                case RequestAttributes.Json:
                    return (r, o, s) =>
                    {

                        using (var ms = new MemoryStream())
                        {
                            JsonDataContractSerializer.Instance.SerializeToStream(o, ms);
                            ms.Position = 0;
                            r.Response.SetContentLength(ms.Length);
                            s.Write(ms.GetBuffer(), 0, (int)ms.Length);
                            s.Flush();
                        }
                    };

Thanks and best regards,
Jose
Reply all
Reply to author
Forward
0 new messages