Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Silverlight, GZip and ServiceStack
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (this.Request.Url.AbsolutePath.Contains("/api/"))
{
this.Request.Filter = new System.IO.Compression.GZipStream(this.Request.Filter, System.IO.Compression.CompressionMode.Decompress, true);
this.Response.Filter = new System.IO.Compression.GZipStream(this.Response.Filter, System.IO.Compression.CompressionMode.Compress, true);
}
}
public class GzipJsonServiceClient : JsonServiceClient
{
public GzipJsonServiceClient(string baseUri) : base(baseUri) { }
public GzipJsonServiceClient(string syncReplyBaseUri, string asyncOneWayBaseUri) : base(syncReplyBaseUri, asyncOneWayBaseUri) { }
public override void SerializeToStream(IRequestContext requestContext, object request, Stream stream)
{
using (var memStream = new MemoryStream())
{
base.SerializeToStream(requestContext, request, memStream);
var zipped = GZipStream.CompressBuffer(memStream.ToArray());
stream.Write(zipped, 0, zipped.Length);
}
}
public override StreamDeserializerDelegate StreamDeserializer
{
get { return UnZipper;}
}
private object UnZipper(Type type, Stream fromStream)
{
using (var unzipped = new GZipStream(fromStream, CompressionMode.Decompress))
return base.StreamDeserializer(type, unzipped);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.