4

There would be an iOS App and the .NET WebApi which would require to sync large data or records.

We need to have in Memory Compression on both iOS and .NET WebApi which would compress and decompress the data and then further process.

On iOS App side, we have planned to use in Memory GZip compression using something like below-

http://www.clintharris.net/2009/how-to-gzip-data-in-memory-using-objective-c/

http://www.deusty.com/2007/07/gzip-compressiondecompression.html

On .Net WebApi side, we have planned to use the GZip compression to be developed using- https://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream%28v=vs.110%29.aspx

Now my question is-

Is it possible to use the GZipStream Class in .Net to compress the JSON string? In .Net data would be fetched from the database into models and then the models would be converted to JSON strings and then that string needs to be compressed before sending to device and same inverse process would be followed when receiving data from the iOS App.

Is there any other best approach then the above which we have planned? Any technical advice, relevant links or some sample code snippet would be very helpful.

The Solution: Thanks to @puneet for leading this post to get a proper solution.

As my server application is a Asp.Net WebAPI and doesn't have any files (html, css, js etc.) to be transferred from the server to browsers like any traditional website. http://forums.asp.net/t/1771770.aspx

Therefore the Automatic IIS Compression would not work in my case, which can be enabled by following the steps given here- http://www.codeproject.com/Articles/186233/Utilize-gzip-compression-in-IIS

In my case both GZip Requests and Responses need to be handled and for this purpose seperate handlers would be required and in that case I am following these below SO posts-

For Compression: Compress HTTP GET Response

For Decompression: How do enable a .Net web-API to accept g-ziped posts

Another mentionable link- http://benfoster.io/blog/aspnet-web-api-compression

Community
  • 1
  • 1
Manik Arora
  • 4,702
  • 1
  • 25
  • 48

1 Answers1

3

Is it possible to use the GZipStream Class in .Net to compress the JSON string? In .Net data would be fetched from the database into models and then the models would be converted to JSON strings and then that string needs to be compressed before sending to device

Yes it is possible by two ways.

  1. Let the IIS handle it itself by enabling Dynamic Compression.
  2. Write your own code to compress the response

Please follow the answer of this thread for both options.

I tried the second way (using the code given in referenced thread answer) and it worked for me very well.

and same inverse process would be followed when receiving data from the iOS App.

Yes, we can decompress the gZipped data. [this blog post] (http://blog.kaliatech.com/2013/02/posting-compressed-json-content-to-asp-net-web-api-controller/) has the code for the same. I haven't tried this one, but it seems fine and should work.

Community
  • 1
  • 1
Puneet
  • 2,051
  • 9
  • 17
  • Thanks a lot for the effort, I think I'm not going with the IIS Compression for this one because it compresses files only as mine are just Asp.Net WebApi, therefore Accept-Encoding will not work, please see these links- http://forums.asp.net/t/1771770.aspx - http://www.codeproject.com/Articles/186233/Utilize-gzip-compression-in-IIS – Manik Arora Mar 04 '15 at 09:11
  • 1
    But using the posts and links which you provided, I would be going for these two implementations, for Decompressing - http://stackoverflow.com/questions/12345292/how-do-enable-a-net-web-api-to-accept-g-ziped-posts/14990735#14990735 and for Compressing - http://stackoverflow.com/questions/10443588/compress-http-get-response – Manik Arora Mar 04 '15 at 09:17
  • I will put up the solution in the question as well, thanks again :) – Manik Arora Mar 04 '15 at 09:17