如何在返回 json 的 Restful WCF 服务中进行压缩

发布于 2024-11-18 22:03:22 字数 744 浏览 2 评论 0原文

我有一个返回 JSON 的宁静 WCF 服务。我想知道如何压缩数据?我读到 HTTP 支持压缩,我只是不知道如何打开它。我有点希望它是一种方法装饰。下面是我的网络服务的代码。理想情况下,我正在寻找一些代码示例或文章来阅读,我一直在谷歌搜索,到目前为止却一无所获,我的 google-foo 今天很弱。

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class FooService
{
    [WebInvoke(UriTemplate = "Foo", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    public string Foo(string aParameter)
    {
        int number = int.Parse(aParameter);
        number++;
        return "I added 1 to your number and got " + number;

    }
}

I have a restful WCF service that is returning JSON. I was wondering how I could compress the data? I read that HTTP has support for compression, I just don't know how to turn it on. I was sort of hoping it would be a method decoration. Below is the code for my webservice. Idealy looking for some code examples or articles to read, I've been googling and so far have come up empty, my google-foo is weak today.

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class FooService
{
    [WebInvoke(UriTemplate = "Foo", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    public string Foo(string aParameter)
    {
        int number = int.Parse(aParameter);
        number++;
        return "I added 1 to your number and got " + number;

    }
}

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

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

发布评论

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

评论(2

梦里的微风 2024-11-25 22:03:23

试试这个 C# 压缩,它在内存压缩方面表现得非常出色!而且它是免费的!
http://www.codeproject.com/KB/cs/IMcompressor.aspx

Try this C# compression, it works like a champ at doing in-memory compression! And it is Free!!
http://www.codeproject.com/KB/cs/IMCompressor.aspx

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