What is the easiest way to add compression to WCF in Silverlight?

拈花ヽ惹草 提交于 2019-11-29 20:59:58

If you are using IIS7, take a look at the Compression Module. This allows you to configure compression for HTTP requests to your server.

Daniel Ballinger

WS-Compression for WCF allows you to configure compression on the binding.

See WS-Compression for WCF by Pablo M. Cibraro

Alternatively, try Microsofts GZip Encoder Sample which "creates an encoder channel that uses the System.IO.Compression.GZipStream class to compress outgoing WCF messages"

I didn't see a native way for WCF to do compression when doing a WCF project recently. I just used the System.IO.Compression namespace and made a quick compressor. Here's the code i used

public static class CompressedSerializer
{
    /// <summary>
    /// Decompresses the specified compressed data.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="compressedData">The compressed data.</param>
    /// <returns></returns>
    public static T Decompress<T>(byte[] compressedData) where T : class
    {
        T result = null;
        using (MemoryStream memory = new MemoryStream())
        {
            memory.Write(compressedData, 0, compressedData.Length);
            memory.Position = 0L;

            using (GZipStream zip= new GZipStream(memory, CompressionMode.Decompress, true))
            {
                zip.Flush();
                var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                result = formatter.Deserialize(zip) as T;
            }
        }

        return result;
    }

    /// <summary>
    /// Compresses the specified data.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="data">The data.</param>
    /// <returns></returns>
    public static byte[] Compress<T>(T data)
    {
        byte[] result = null;
        using (MemoryStream memory = new MemoryStream())
        {
            using (GZipStream zip= new GZipStream(memory, CompressionMode.Compress, true))
            {
                var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                formatter.Serialize(zip, data);
            }

            result = memory.ToArray();
        }

        return result;
    }
}

then i just had my services take in a byte array as an input, like such

void ReceiveData(byte[] data);

Worked out well for me.

Mike H.

It should also be noted that you may need to add the mime type to applicationHost.config under <httpCompression><dynamicTypes> section in addition to enabling compression for the site:

<add mimeType="application/soap+msbin1" enabled="true" />

If certain dynamic responses are not being compressed (and some are) it could be a mime type issue. Use Fiddler to get the specifics associated with the request. Failed request tracing may be useful in determining whether or not IIS is even attempting to compression the response. If compression is correctly configured you will see a NO_MATCHING_CONTENT_TYPE in the complete trace section of the trace output.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!