I want to compress the request sent from a client.
I've found the Q/A: ServiceStack - How To Compress Requests From Client
But when using this code I get a SerializationException from the server that the content should start with a '{' and not '\u001F...'
Is this solution still valid or is there another way to compress client request payload?
UPDATE 1: Here is the output from Fiddler. Request:
POST http://xxxxxx:8104/entries HTTP/1.1
Accept: application/json
User-Agent: ServiceStack .NET Client 4,51
Accept-Encoding: gzip,deflate
Content-Encoding: gzip
Content-Type: application/json
Host: xxxxxx:8104
Content-Length: 187
Expect: 100-continue
[binary data not shown here]
And the response:
HTTP/1.1 400 Bad Request
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Tue, 03 Jan 2017 07:22:57 GMT
427
{"ResponseStatus":{"ErrorCode":"SerializationException","Message":"Could not deserialize 'application/json' request using Namespace.NameOfDto'\nError: System.Runtime.Serialization.SerializationException: Type definitions should start with a '{', expecting serialized type 'NameOfDto', got string starting with: \u001F \b\u0000\u0000\u0000\u0000\u0000\u0004\u00005 \n @\u0010 e 1 P W :h\u001D :D M'YЙ \u001D B| F 7 \r\n at ServiceStack.Text.Common.DeserializeTypeRefJson.StringToType(TypeConfig typeConfig, String strType, EmptyCtorDelegate ctorFn, Dictionary`2 typeAccessorMap)\r\n at ServiceStack.Text.Common.DeserializeType`1.<>c__DisplayClass1_0.<GetParseMethod>b__1(String value)\r\n at ServiceStack.Text.JsonSerializer.DeserializeFromString(String value, Type type)\r\n at ServiceStack.Text.JsonSerializer.DeserializeFromStream(Type type, Stream stream)\r\n at ServiceStack.Serialization.JsonDataContractSerializer.DeserializeFromStream(Type type, S
27e
tream stream)\r\n at ServiceStack.Host.Handlers.ServiceStackHandlerBase.CreateContentTypeRequest(IRequest httpReq, Type requestType, String contentType)","StackTrace":" at ServiceStack.Host.Handlers.ServiceStackHandlerBase.CreateContentTypeRequest(IRequest httpReq, Type requestType, String contentType)\r\n at ServiceStack.Host.RestHandler.CreateRequest(IRequest httpReq, IRestPath restPath, Dictionary`2 requestParams)\r\n at ServiceStack.Host.RestHandler.CreateRequest(IRequest httpReq, IRestPath restPath)\r\n at ServiceStack.Host.RestHandler.ProcessRequestAsync(IRequest httpReq, IResponse httpRes, String operationName)"}}
0
Client:
public class GzipJsonServiceClient : JsonServiceClient
{
public GzipJsonServiceClient()
{
SetRequestFilter();
}
public GzipJsonServiceClient(string baseUri)
: base(baseUri)
{
SetRequestFilter();
}
public override void SerializeToStream(IRequest requestContext, object request, Stream stream)
{
using (var gzipStream = new GZipStream(stream, CompressionMode.Compress))
{
base.SerializeToStream(requestContext, request, gzipStream);
gzipStream.Close();
}
}
private void SetRequestFilter()
{
RequestFilter = req =>
{
if (req.Method.HasRequestBody())
{
req.Headers.Add(HttpRequestHeader.ContentEncoding, CompressionTypes.GZip);
}
};
}
}
Request code:
var client = new GzipJsonServiceClient(uri) { Timeout = TimeSpan.FromSeconds(10) };
var request = new NameOfDto();
client.Post(request);
The service side is from Visual Studio template, hosting ServiceStack service inside Windows service. It's pretty vanilla, with one method which isn't reached:
public void Post(NameOfDto request)
{
var appHost = (AppHost)HostContext.AppHost;
...
}
Support for client Gzip + Deflate compression has been added to ServiceStack HttpListener Server and all C# Service Clients in this commit.
This lets you send client requests with the new
RequestCompressionType
property, e.g:This feature is available from v4.5.5+ that's now available on MyGet.
OK, so I figured out a way to solve my issue, using Newtonsoft JSON nuget. Though I would prefer if there was an "out-of-the-box" solution with ServiceStack in which I didn't have to modify the DTO.
I added the IRequiresRequestStream interface to my DTO:
Then created new class:
Finally updated the end-point: