Search code examples
.netwcfjsonwcf-rest

How to do compression in a restful wcf service returning json


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;

    }
}

Solution

  • You can add GZip compression to WCF based REST enabled service.

    Here's how.