在 Silverlight 中向 WCF 添加压缩的最简单方法是什么?

发布于 2024-07-04 04:35:46 字数 941 浏览 6 评论 0原文

我有一个访问 WCF Web 服务的 silverlight 2 beta 2 应用程序。 因此,目前只能使用 basicHttp 绑定。 Web 服务将返回相当大量的 XML 数据。 从带宽使用的角度来看,这似乎相当浪费,因为如果压缩,响应会小 5 倍(我实际上将响应粘贴到 txt 文件中并对其进行压缩。)。

该请求确实有“Accept-Encoding:gzip,deflate” - 有没有办法让WCF服务gzip(或以其他方式压缩)响应?

我确实找到了这个 链接 但它恕我直言,对于应该开箱即用处理的功能来说,确实似乎有点复杂。

好的 - 起初我使用 System.IO.Compression 标记解决方案作为答案,因为我永远无法“似乎”让 IIS7 动态压缩发挥作用。 事实证明:

  1. IIS7 上的动态压缩一直一直有效。 只是 Nikhil 的 IE Web Developer Helper 插件没有显示它工作。 我的猜测是,由于 SL 将 Web 服务调用交给浏览器,因此浏览器会“在幕后”处理它,而 Nikhil 的工具永远不会看到压缩的响应。 我可以通过使用 Fiddler 来确认这一点,Fiddler 监视浏览器应用程序外部的流量。 在 fiddler 中,响应实际上是 gzip 压缩!!

  2. System.IO.Compression 解决方案的另一个问题是 System.IO.Compression 在 Silverlight CLR 中不存在。

因此,从我的角度来看,在 Silverlight 中启用 WCF 压缩的最简单方法是在 IIS7 中启用动态压缩并且根本不编写任何代码。

I have a silverlight 2 beta 2 application that accesses a WCF web service. Because of this, it currently can only use basicHttp binding. The webservice will return fairly large amounts of XML data. This seems fairly wasteful from a bandwidth usage standpoint as the response, if zipped, would be smaller by a factor of 5 (I actually pasted the response into a txt file and zipped it.).

The request does have the "Accept-Encoding: gzip, deflate" - Is there any way have the WCF service gzip (or otherwise compress) the response?

I did find this link but it sure seems a bit complex for functionality that should be handled out-of-the-box IMHO.

OK - at first I marked the solution using the System.IO.Compression as the answer as I could never "seem" to get the IIS7 dynamic compression to work. Well, as it turns out:

  1. Dynamic Compression on IIS7 was working al along. It is just that Nikhil's Web Developer Helper plugin for IE did not show it working. My guess is that since SL hands the web service call off to the browser, that the browser handles it "under the covers" and Nikhil's tool never sees the compressed response. I was able to confirm this by using Fiddler which monitors traffic external to the browser application. In fiddler, the response was, in fact, gzip compressed!!

  2. The other problem with the System.IO.Compression solution is that System.IO.Compression does not exist in the Silverlight CLR.

So from my perspective, the EASIEST way to enable WCF compression in Silverlight is to enable Dynamic Compression in IIS7 and write no code at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终难愈 2024-07-11 04:35:47

如果您使用的是 IIS7,请查看 压缩模块。 这允许您为服务器的 HTTP 请求配置压缩。

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

德意的啸 2024-07-11 04:35:47

最近做WCF项目的时候没有看到WCF原生的方式来做压缩。 我只是使用 System.IO.Compression 命名空间并制作了一个快速压缩器。 这是我使用的代码

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;
    }
}

,然后我只是让我的服务接受字节数组作为输入,就像这样

void ReceiveData(byte[] data);

对我来说效果很好。

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.

陌路终见情 2024-07-11 04:35:47

还应该注意的是,除了为站点启用压缩之外,您可能还需要将 mime 类型添加到 部分下的 applicationHost.config

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

如果某些动态响应没有被压缩(有些是),则可能是 MIME 类型问题。 使用 Fiddler 获取与请求相关的详细信息。 失败的请求跟踪可能有助于确定 IIS 是否正在尝试压缩响应。 如果压缩配置正确,您将在跟踪输出的完整跟踪部分看到NO_MATCHING_CONTENT_TYPE

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.

饮惑 2024-07-11 04:35:46

WCF 的 WS-压缩允许您在绑定上配置压缩。

请参阅 Pablo M. Cibraro 的 WS-Compression for WCF

或者,尝试 Microsoft < a href="http://msdn.microsoft.com/en-us/library/cc138373.aspx" rel="nofollow noreferrer">GZip 编码器示例 “创建使用 System.IO 的编码器通道.Compression.GZipStream 类用于压缩传出的 WCF 消息”

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文