Skip to content

Commit

Permalink
Changes needed to use Brotli in ASP.NET Core
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher.demicoli committed Jul 29, 2017
1 parent 61d6d26 commit 5605681
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
9 changes: 7 additions & 2 deletions Brotli-ASP.NET-Core-Compression.sln
Original file line number Original file line Diff line number Diff line change
@@ -1,9 +1,14 @@


Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15 # Visual Studio 15
VisualStudioVersion = 15.0.26228.4 VisualStudioVersion = 15.0.26430.16
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Brotli-ASP.NET-Core-Compression", "Brotli-ASP.NET-Core-Compression\Brotli-ASP.NET-Core-Compression.csproj", "{FABF9A3D-A219-486C-B912-0B479ECF2208}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Brotli-ASP.NET-Core-Compression", "Brotli-ASP.NET-Core-Compression\Brotli-ASP.NET-Core-Compression.csproj", "{FABF9A3D-A219-486C-B912-0B479ECF2208}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F053F5A9-7BA6-4C62-A76A-2B4515EC4DBD}"
ProjectSection(SolutionItems) = preProject
NuGet.Config = NuGet.Config
EndProjectSection
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">


<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework> <TargetFramework>netcoreapp1.1</TargetFramework>
Expand All @@ -11,9 +11,11 @@
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" /> <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" /> <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" /> <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.ResponseCompression" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" /> <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" /> <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" /> <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
<PackageReference Include="System.IO.Compression.Brotli" Version="0.1.0-e170729-1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" /> <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
Expand Down
20 changes: 20 additions & 0 deletions Brotli-ASP.NET-Core-Compression/BrotliCompressionProvider.cs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.AspNetCore.ResponseCompression;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;

namespace Brotli_ASP.NET_Core_Compression
{
public class BrotliCompressionProvider : ICompressionProvider
{
public string EncodingName => "br";
public bool SupportsFlush => true;
public Stream CreateStream(Stream outputStream)
{
return new BrotliStream(outputStream, CompressionMode.Compress);
}
}
}
8 changes: 8 additions & 0 deletions Brotli-ASP.NET-Core-Compression/Startup.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.ResponseCompression;


namespace Brotli_ASP.NET_Core_Compression namespace Brotli_ASP.NET_Core_Compression
{ {
Expand All @@ -29,6 +30,12 @@ public void ConfigureServices(IServiceCollection services)
{ {
// Add framework services. // Add framework services.
services.AddMvc(); services.AddMvc();

services.AddResponseCompression(options =>
{
options.Providers.Add<BrotliCompressionProvider>();
options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "image/svg+xml" });
});
} }


// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -47,6 +54,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
app.UseExceptionHandler("/Home/Error"); app.UseExceptionHandler("/Home/Error");
} }


app.UseResponseCompression();
app.UseStaticFiles(); app.UseStaticFiles();


app.UseMvc(routes => app.UseMvc(routes =>
Expand Down
5 changes: 5 additions & 0 deletions NuGet.Config
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
<configuration>
<packageSources>
<add key="dotnet.myget.org dotnet-corefxlab" value="https://dotnet.myget.org/F/dotnet-corefxlab/" />
</packageSources>
</configuration>

0 comments on commit 5605681

Please sign in to comment.