1

I tried to compress the response of service stack using global filters but it not work throws 500 err code. here are my code

this.GlobalResponseFilters.Add((req, response, requestDto) =>
            {
                response.AddHeader("Content-Encoding", "gzip");
                MemoryStream stream = new MemoryStream();
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, requestDto);
                var data = new GZipStream(stream, CompressionMode.Compress);
                response.WriteToResponse(response, "application/json");
}

Here i tried [CompressResponse] attribute also

MvcController

httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = httpClient.GetAsync(url).Result;

ServiceStack.ServiceInterface

public interface ICommonDAL
{
 Task<string> Getdefaultvalue(Getvalue request);
 }

[CompressResponse]

public class CommonDAL : DBConnection, ICommonDAL {

    public async Task<string> Getdefaultvalue(Getvalue request)
    {
        return await WithConnection(async c =>
        {
            var p = new DynamicParameters();
            p.Add("@pID", request.ID);
            p.Add("@pMode", request.Mode);
            var res= await c.QuerySingleAsync<string>("[dbo].[spdemo]", p, commandType: CommandType.StoredProcedure);
            return res
        });
    }

}

Prabhu
  • 13
  • 2

1 Answers1

1

Please refer to ServiceStack Compression Docs, e.g. you can compress Service Responses with the [CompressResponse] attribute, e.g:

[CompressResponse]
public class CompressedServices : Service
{
    public object Any(CompressDto request) => new CompressExamplesResponse(); 
    public object Any(CompressString request) => "foo"; 
    public object Any(CompressBytes request) => "foo".ToUtf8Bytes(); 
    public object Any(CompressStream request) => new MemoryStream("foo".ToUtf8Bytes()); 
    public object Any(CompressFile request) => new HttpResult(VirtualFileSources.GetFile("/foo"));

    public object Any(CompressAnyHttpResult request)
    {
        return new HttpResult(new CompressExamplesResponse());    // DTO
        return new HttpResult("foo", "text/plain");               // string
        return new HttpResult("foo".ToUtf8Bytes(), "text/plain"); // bytes
        //etc
    }
}

Http Headers enter image description here

Prabhu
  • 13
  • 2
mythz
  • 141,670
  • 29
  • 246
  • 390
  • [CompressResponse] Attribute does not make any change in the response contentlength.The length count same as previous count – Prabhu Jun 10 '21 at 09:49
  • @Prabhu please update your question with the raw HTTP Headers and your C# service impl – mythz Jun 10 '21 at 09:52
  • https://stackoverflow.com/a/67918996/16183656 this is my code – Prabhu Jun 10 '21 at 10:08
  • @Prabhu you should update your question as mods are going to delete non answers. Please include the HTTP Headers in your question (e.g using Chrome Web Inspector or Fiddler), only that will show if the response was compressed or not. – mythz Jun 10 '21 at 10:18
  • In browser Response is compressed for that i used Gzip compressor in filters.But here i try to compressor response of stackservice to controller – Prabhu Jun 10 '21 at 10:36
  • @Prabhu Compression is used for reducing the payload size retuned to HTTP Clients, it makes no sense to do any serialization & compression of in memory models within the same process, it has 0 benefit and only adds additional CPU Resources & memory overhead. – mythz Jun 10 '21 at 10:44