• This repository has been archived on 09/Sep/2022
  • Stars
    star
    194
  • Rank 199,321 (Top 4 %)
  • Language
    C#
  • License
    Apache License 2.0
  • Created over 10 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

Compression support for ASP.NET WebAPI and HttpClient

Build status

Drop-in module for ASP.Net WebAPI that enables GZip and Deflate support.
This module is based on this blog post by Ben Foster which in turn is based on this blog post by Kiran Challa.
This code improves on their work by adding several new options, as well as fixing some issues with the original code.

Package Version Downloads
Microsoft.AspNet.WebApi.Extensions.Compression.Server NuGet Package Version NuGet Package Downloads
System.Net.Http.Extensions.Compression.Client 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 ClientCompressionHandler(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 larger than 860 bytes.
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.

Disable compression for endpoint

It is possible to disable compression for a specific endpoint. Just add the [Compression(Enabled = false)] attribute to your endpoint method. (Or the whole controller if you want to disable for all endpoints in it)

OWIN Authentication

When using the OWIN Authentication pipeline, you might encounter errors saying that Server cannot append header after http headers have been sent. This is a bug with OWIN, but as of this moment it has not been fixed.

The workaround is to install the Microsoft.AspNet.WebApi.Extensions.Compression.Server.Owin package and use the included OwinServerCompressionHandler instead of the default ServerCompressionHandler. This class contains code to detect whether the headers have been sent already and prevent any attempts at compression.

Version history

2.0.3 (current)

2.0.0

1.3.0

  • Added attribute for disable compression for certain routes
  • Fixed clearing of non-standard properties when compressing and decompressing

1.2.2

  • Stop trying to compress requests/responses with no content

1.2.1

  • Properly copy HTTP headers from the content that is going to be compressed

1.2.0

  • Fixed 504 timeout error when returning ByteArrayContent from an ApiController
  • Fixed bug with content stream sometimes being disposed before returning
  • Added better test coverage

1.1.2

  • Changed default threshold for compression to 860 bytes (what Akamai uses)
  • Now reports proper Content-Length

1.1.0

  • Simplified usage
  • Added support for setting a minimum content size for compressing

1.0.3

  • First release, basic compression of server responses and client requests
  • Did not support compressing POSTs and PUTs

More Repositories

1

sanity-plugin-tabs

Input component for rendering fieldsets as tabs
JavaScript
33
star
2

Sentinel.OAuth

An OAuth server based on the OWIN OAuth 2.0 Authorization Server
C#
20
star
3

Microsoft.AspNet.OData.Extensions.ODataQueryMapper

A query mapper for OData v4.0
C#
16
star
4

WCFServiceProxy

Service proxy for interacting with WCF services.
C#
10
star
5

playready-windows10-sample

Sample project for creating a video player capable of playing a PlayReady protected stream
C#
10
star
6

Microsoft.AspNet.WebApi.HelpPage.Ex

Extensions for Microsoft ASP.NET Web API Help Pages
C#
8
star
7

umbraco.slashbase

/Base 2.0
JavaScript
6
star
8

Reactive.EventAggregator.Net35

Event Aggregator using Reactive Extensions 1.0, comptable with .NET 3.5 applications
5
star
9

plate-tailwindcss

Sample code for styling @udecode/plate with TailwindCSS
TypeScript
4
star
10

AcronymFactory

C# helper class for generating acronyms
C#
3
star
11

provision

An easy-to-use and fast caching system for .NET with support for MemoryCache, Redis
C#
3
star
12

angular-heremaps

AngularJS Provider for working with the HERE Maps API
JavaScript
3
star
13

Ninject.Extensions.MissingBindingLogger

Logs missing bindings in your application
C#
2
star
14

vscode-monorepo-debug

Config for debugging a monorepo using Turbo, Next.js, Typescript and tsup
TypeScript
2
star
15

angular-promise-monitor

An Angular module for monitoring promises
JavaScript
2
star
16

Yuml.Net

Model diagram generator for .NET using yUML.me
C#
1
star
17

WebApi.DocumentationGenerator

Documentation pages generator for ASP.NET WebAPI
JavaScript
1
star
18

sanity-plugin-tabs-demo

Demo project for showing off sanity-plugin-tabs
JavaScript
1
star
19

ArgumentContracts

1
star
20

Ninject.Extensions.WebApi.UsageLogger

Logger for WebAPI controller usage
JavaScript
1
star