diff --git a/samples/ResponseCompressionSample/Startup.cs b/samples/ResponseCompressionSample/Startup.cs index 17dd97bd39..142c8c1012 100644 --- a/samples/ResponseCompressionSample/Startup.cs +++ b/samples/ResponseCompressionSample/Startup.cs @@ -18,8 +18,8 @@ namespace ResponseCompressionSample { public void ConfigureServices(IServiceCollection services) { - services.AddTransient(); - services.AddTransient(); + services.AddSingleton(); + services.AddSingleton(); services.AddResponseCompression("text/plain", "text/html"); } diff --git a/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionBuilderExtensions.cs b/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionBuilderExtensions.cs new file mode 100644 index 0000000000..774aa5049a --- /dev/null +++ b/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionBuilderExtensions.cs @@ -0,0 +1,30 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNetCore.ResponseCompression; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; + +namespace Microsoft.AspNetCore.Builder +{ + /// + /// Extension methods for the ResponseCompression middleware. + /// + public static class ResponseCompressionBuilderExtensions + { + /// + /// Adds middleware for dynamically compressing HTTP Responses. + /// + /// The instance this method extends. + public static IApplicationBuilder UseResponseCompression(this IApplicationBuilder builder) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + return builder.UseMiddleware(); + } + } +} diff --git a/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionMiddleware.cs b/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionMiddleware.cs index 692d77f643..b6841454d2 100644 --- a/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionMiddleware.cs +++ b/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionMiddleware.cs @@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.ResponseCompression private readonly IResponseCompressionProvider _provider; - private readonly bool _enableHttps; + private readonly bool _enableForHttps; /// /// Initialize the Response Compression middleware. @@ -43,7 +43,7 @@ namespace Microsoft.AspNetCore.ResponseCompression _next = next; _provider = provider; - _enableHttps = options.Value.EnableHttps; + _enableForHttps = options.Value.EnableForHttps; } /// @@ -55,7 +55,7 @@ namespace Microsoft.AspNetCore.ResponseCompression { ICompressionProvider compressionProvider = null; - if (!context.Request.IsHttps || _enableHttps) + if (!context.Request.IsHttps || _enableForHttps) { compressionProvider = _provider.GetCompressionProvider(context); } diff --git a/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionOptions.cs b/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionOptions.cs index 7d3b049984..e8d492efbd 100644 --- a/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionOptions.cs +++ b/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionOptions.cs @@ -19,6 +19,6 @@ namespace Microsoft.AspNetCore.ResponseCompression /// Indicates if responses over HTTPS connections should be compressed. The default is 'false'. /// Enable compression on HTTPS connections may expose security problems. /// - public bool EnableHttps { get; set; } = false; + public bool EnableForHttps { get; set; } = false; } } diff --git a/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionProvider.cs b/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionProvider.cs index 32376413c1..c37cfdcc66 100644 --- a/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionProvider.cs +++ b/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionProvider.cs @@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.ResponseCompression if (options.Value.MimeTypes == null || !options.Value.MimeTypes.Any()) { - throw new InvalidOperationException("No mime types specified"); + throw new InvalidOperationException("No MIME types specified"); } _mimeTypes = new HashSet(options.Value.MimeTypes, StringComparer.OrdinalIgnoreCase); } diff --git a/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionExtensions.cs b/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionServicesExtensions.cs similarity index 76% rename from src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionExtensions.cs rename to src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionServicesExtensions.cs index cb827c4fd1..6cd04a132c 100644 --- a/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionExtensions.cs +++ b/src/Microsoft.AspNetCore.ResponseCompression/ResponseCompressionServicesExtensions.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Builder /// /// Extension methods for the ResponseCompression middleware. /// - public static class ResponseCompressionExtensions + public static class ResponseCompressionServicesExtensions { /// /// Add response compression services and enable compression for responses with the given MIME types. @@ -45,22 +45,8 @@ namespace Microsoft.AspNetCore.Builder } services.Configure(configureOptions); - services.TryAddTransient(); + services.TryAddSingleton(); return services; } - - /// - /// Adds middleware for dynamically compressing HTTP Responses. - /// - /// The instance this method extends. - public static IApplicationBuilder UseResponseCompression(this IApplicationBuilder builder) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - return builder.UseMiddleware(); - } } } diff --git a/test/Microsoft.AspNetCore.ResponseCompression.Tests/ResponseCompressionMiddlewareTest.cs b/test/Microsoft.AspNetCore.ResponseCompression.Tests/ResponseCompressionMiddlewareTest.cs index 8f468d8b03..08e3f88138 100644 --- a/test/Microsoft.AspNetCore.ResponseCompression.Tests/ResponseCompressionMiddlewareTest.cs +++ b/test/Microsoft.AspNetCore.ResponseCompression.Tests/ResponseCompressionMiddlewareTest.cs @@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests { var options = new ResponseCompressionOptions(); - Assert.False(options.EnableHttps); + Assert.False(options.EnableForHttps); } [Fact] @@ -256,7 +256,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests { services.AddResponseCompression(options => { - options.EnableHttps = enableHttps; + options.EnableForHttps = enableHttps; options.MimeTypes = new[] { TextPlain }; }); })