Skip to content

Use dynamic compression to reduce bandwidth usage

When network bandwidth can be a bottleneck, you can enabling dynamic compression on the OData service.

Dynamic compression compresses dynamically generated content at the server prior to sending the content to the client browser. The client browser will then decompress the content upon reception. This results in smaller amounts of data being sent over the network which can reduce overall bandwidth usage. Using dynamic compression will result a small increase in CPU usage at the server for each processed request.

Once the OData service is correctly set up for dynamic compression, you must include requests for compressed content when you send requests to the OData service.

You can do this by adding a line in a SendingRequest2 event handler on the DataServiceContext. See the example below.

context.SendingRequest2 += (sender, args) =>

{

if (!arg.IsBatchPart)

{

// Tell the OData server that you accept compressed content. You can do this even if the server has not been set up for dynamic compression, the server will just send uncompressed content.

((HttpWebRequestMessage)args.RequestMessage).HttpWebRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

}

};