如何在返回 json 的 Restful WCF 服务中进行压缩
我有一个返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个 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
您可以将 GZip 压缩添加到基于 WCF 且启用 REST 的服务。
方法如下。
You can add GZip compression to WCF based REST enabled service.
Here's how.