Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GZip compression in WCF WebService

I have a .NET 3.5 Webservice Hosted on IIS7.5.

I have a client application who connects to this webservice.

I changed (in client application) the httpWebRequest.Create method to add automaticDecompression for GZip but it isn't working

 WebRequest IWebRequestCreate.Create(Uri uri)
    {
        HttpWebRequest httpWebRequest =
            Activator.CreateInstance(
                typeof(HttpWebRequest), 
                BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
                null, 
                new object[] { uri, null }, 
                null) as HttpWebRequest;

        if (httpWebRequest == null)
            return null;

        httpWebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
        httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;


        return httpWebRequest;
    }

In this way the request is sent correctly, the answer is encoded in gzip (I see it from Fiddler), but an exception occurs:

Response is not wellformed XML

(I think the client doesn't decode the answer)

If I remove this row, as in MSDN documentation

httpWebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");

The answer is not GZip encoded (and in the request there's no ACCEPT-ENCODING header)

like image 969
AndreaCi Avatar asked Apr 12 '12 14:04

AndreaCi


1 Answers

I've done this to transfer DataTable objects using WCF with DataContract. You have to create the DataContract as follows:

[DataContract]
public class WCFDataTableContract
{
    [DataMember]
    public byte[] Schema { get; set; }

    [DataMember]
    public byte[] Data { get; set; }
}

Then I created a Binary Converter that will automatically convert any object to a byte array that I can then compress using GZip.

public static class CompressedBinaryConverter
{
    /// <summary>
    /// Converts any object into a byte array and then compresses it
    /// </summary>
    /// <param name="o">The object to convert</param>
    /// <returns>A compressed byte array that was the object</returns>
    public static byte[] ToByteArray(object o)
    {
        if (o == null)
            return new byte[0];

        using (MemoryStream outStream = new MemoryStream())
        {
            using (GZipStream zipStream = new GZipStream(outStream, CompressionMode.Compress))
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    new BinaryFormatter().Serialize(stream, o);
                    stream.Position = 0;
                    stream.CopyTo(zipStream);
                    zipStream.Close();
                    return outStream.ToArray();
                }
            }
        }
    }

    /// <summary>
    /// Converts a byte array back into an object and uncompresses it
    /// </summary>
    /// <param name="byteArray">Compressed byte array to convert</param>
    /// <returns>The object that was in the byte array</returns>
    public static object ToObject(byte[] byteArray)
    {
        if (byteArray.Length == 0)
            return null;

        using (MemoryStream decomStream = new MemoryStream(byteArray), ms = new MemoryStream())
        {
            using (GZipStream hgs = new GZipStream(decomStream, CompressionMode.Decompress))
            {
                hgs.CopyTo(ms);
                decomStream.Close();
                hgs.Close();
                ms.Position = 0;
                return new BinaryFormatter().Deserialize(ms);
            }
        }
    }
}

Dump this in your project and call like this in your Service to compress:

dt.Data = CompressedBinaryConverter.ToByteArray(data);

Then call it like this on your client side to convert back to an object:

dt = (DataTable)CompressedBinaryConverter.ToObject(wdt.Data);
like image 143
MrWuf Avatar answered Oct 03 '22 22:10

MrWuf