Code Monkey home page Code Monkey logo

microsoft.aspnet.webapi.messagehandlers.compression's Introduction

Microsoft.AspNet.WebApi.MessageHandlers.Compression

Drop-in module for ASP.Net WebAPI that enables GZip and Deflate support

NuGet Package Version  NuGet Package Downloads

How to use

Server side

You need to add the compression handler as the last applied message handler on outgoing requests, and the first one on incoming requests.
To do that, just add the following line to your App_Start\WebApiConfig.cs file after adding all your other message handlers:

GlobalConfiguration.Configuration.MessageHandlers.Insert(0, new ServerCompressionHandler(new GZipCompressor(), new DeflateCompressor()));

This will insert the ServerCompressionHandler to the request pipeline as the first on incoming requests, and the last on outgoing requests.

Client side

JavaScript

If you are doing your requests with JavaScript you probably don't have to do do anything.
Just make sure the gzip and deflate values are included in the Accept-Encoding header. (Most browsers do this by default)

C#

You need to apply the following code when creating your HttpClient.

var client = new HttpClient(new ClientompressionHandler(new GZipCompressor(), new DeflateCompressor()));

client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));

Thats it! You should now immediately start experiencing smaller payloads when doing GET, POST, PUT, etc.

Advanced use

Skip compression of requests/responses that are smaller than a specified value

By default, both ServerCompressionHandler and ClientCompressionHandler compress everything.
However, this can be overriden by inserting a threshold as the first parameter like this:

var serverCompressionHandler = new ServerCompressionHandler(4096, new GZipCompressor(), new DeflateCompressor());
var clientCompressionHandler = new ClientCompressionHandler(4096, new GZipCompressor(), new DeflateCompressor());

The above code will skip compression for any request/response that is smaller than 4096 bytes / 4 kB.

Version history

1.2.1 (current)

  • Properly copy HTTP headers from the content that is going to be compressed
  • Fixed 504 timeout error when returning ByteArrayContent from an ApiController
  • Fixed bug wuth content stream sometimes being disposed before returning
  • Added better test coverage
  • Changed default threshold for compression to 860 bytes (what Akamai uses)
  • Now reports proper Content-Length
  • Simplified usage
  • Added support for setting a minimum content size for compressing
  • First release, basic compression of server responses and client requests
  • Did not support compressing POSTs and PUTs

microsoft.aspnet.webapi.messagehandlers.compression's People

Contributors

azzlack avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.