diff --git a/src/Microsoft.AspNetCore.Cors/Infrastructure/CorsMiddleware.cs b/src/Microsoft.AspNetCore.Cors/Infrastructure/CorsMiddleware.cs index f71c198de7..0fcd4b947b 100644 --- a/src/Microsoft.AspNetCore.Cors/Infrastructure/CorsMiddleware.cs +++ b/src/Microsoft.AspNetCore.Cors/Infrastructure/CorsMiddleware.cs @@ -17,7 +17,6 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure { private readonly Func OnResponseStartingDelegate = OnResponseStarting; private readonly RequestDelegate _next; - private readonly ICorsPolicyProvider _corsPolicyProvider; private readonly CorsPolicy _policy; private readonly string _corsPolicyName; @@ -26,61 +25,12 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure /// /// The next middleware in the pipeline. /// An instance of . - /// A policy provider which can get an . - [Obsolete("This constructor has been replaced with an equivalent constructor which requires an ILoggerFactory")] - public CorsMiddleware( - RequestDelegate next, - ICorsService corsService, - ICorsPolicyProvider policyProvider) - : this(next, corsService, policyProvider, NullLoggerFactory.Instance, policyName: null) - { - } - - /// - /// Instantiates a new . - /// - /// The next middleware in the pipeline. - /// An instance of . - /// A policy provider which can get an . - /// An optional name of the policy to be fetched. - [Obsolete("This constructor has been replaced with an equivalent constructor which requires an ILoggerFactory")] - public CorsMiddleware( - RequestDelegate next, - ICorsService corsService, - ICorsPolicyProvider policyProvider, - string policyName) - : this(next, corsService, policyProvider, NullLoggerFactory.Instance, policyName) - { - } - - /// - /// Instantiates a new . - /// - /// The next middleware in the pipeline. - /// An instance of . - /// An instance of the which can be applied. - [Obsolete("This constructor has been replaced with an equivalent constructor which requires an ILoggerFactory")] - public CorsMiddleware( - RequestDelegate next, - ICorsService corsService, - CorsPolicy policy) - : this(next, corsService, policy, NullLoggerFactory.Instance) - { - } - - /// - /// Instantiates a new . - /// - /// The next middleware in the pipeline. - /// An instance of . - /// A policy provider which can get an . /// An instance of . public CorsMiddleware( RequestDelegate next, ICorsService corsService, - ICorsPolicyProvider policyProvider, ILoggerFactory loggerFactory) - : this(next, corsService, policyProvider, loggerFactory, policyName: null) + : this(next, corsService, loggerFactory, policyName: null) { } @@ -89,13 +39,11 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure /// /// The next middleware in the pipeline. /// An instance of . - /// A policy provider which can get an . /// An instance of . /// An optional name of the policy to be fetched. public CorsMiddleware( RequestDelegate next, ICorsService corsService, - ICorsPolicyProvider policyProvider, ILoggerFactory loggerFactory, string policyName) { @@ -109,11 +57,6 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure throw new ArgumentNullException(nameof(corsService)); } - if (policyProvider == null) - { - throw new ArgumentNullException(nameof(policyProvider)); - } - if (loggerFactory == null) { throw new ArgumentNullException(nameof(loggerFactory)); @@ -121,7 +64,6 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure _next = next; CorsService = corsService; - _corsPolicyProvider = policyProvider; _corsPolicyName = policyName; Logger = loggerFactory.CreateLogger(); } @@ -170,19 +112,19 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure private ILogger Logger { get; } /// - public Task Invoke(HttpContext context) + public Task Invoke(HttpContext context, ICorsPolicyProvider corsPolicyProvider) { if (!context.Request.Headers.ContainsKey(CorsConstants.Origin)) { return _next(context); } - return InvokeCore(context); + return InvokeCore(context, corsPolicyProvider); } - private async Task InvokeCore(HttpContext context) + private async Task InvokeCore(HttpContext context, ICorsPolicyProvider corsPolicyProvider) { - var corsPolicy = _policy ?? await _corsPolicyProvider?.GetPolicyAsync(context, _corsPolicyName); + var corsPolicy = _policy ?? await corsPolicyProvider.GetPolicyAsync(context, _corsPolicyName); if (corsPolicy == null) { Logger?.NoCorsPolicyFound(); diff --git a/src/Microsoft.AspNetCore.Cors/breakingchanges.netcore.json b/src/Microsoft.AspNetCore.Cors/breakingchanges.netcore.json new file mode 100644 index 0000000000..e30bbaf3a8 --- /dev/null +++ b/src/Microsoft.AspNetCore.Cors/breakingchanges.netcore.json @@ -0,0 +1,22 @@ +[ + { + "TypeId": "public class Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware", + "MemberId": "public .ctor(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.AspNetCore.Cors.Infrastructure.ICorsService corsService, Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicy policy)", + "Kind": "Removal" + }, + { + "TypeId": "public class Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware", + "MemberId": "public .ctor(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.AspNetCore.Cors.Infrastructure.ICorsService corsService, Microsoft.AspNetCore.Cors.Infrastructure.ICorsPolicyProvider policyProvider)", + "Kind": "Removal" + }, + { + "TypeId": "public class Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware", + "MemberId": "public .ctor(Microsoft.AspNetCore.Http.RequestDelegate next, Microsoft.AspNetCore.Cors.Infrastructure.ICorsService corsService, Microsoft.AspNetCore.Cors.Infrastructure.ICorsPolicyProvider policyProvider, System.String policyName)", + "Kind": "Removal" + }, + { + "TypeId": "public class Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware", + "MemberId": "public System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context)", + "Kind": "Removal" + } + ] diff --git a/test/Microsoft.AspNetCore.Cors.Test/CorsMiddlewareTests.cs b/test/Microsoft.AspNetCore.Cors.Test/CorsMiddlewareTests.cs index 7d80e29fba..4aac15de72 100644 --- a/test/Microsoft.AspNetCore.Cors.Test/CorsMiddlewareTests.cs +++ b/test/Microsoft.AspNetCore.Cors.Test/CorsMiddlewareTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -336,7 +336,6 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure var middleware = new CorsMiddleware( Mock.Of(), corsService, - mockProvider.Object, loggerFactory, policyName: null); @@ -344,7 +343,7 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure httpContext.Request.Headers.Add(CorsConstants.Origin, new[] { "http://example.com" }); // Act - await middleware.Invoke(httpContext); + await middleware.Invoke(httpContext, mockProvider.Object); // Assert mockProvider.Verify( @@ -366,7 +365,6 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure var middleware = new CorsMiddleware( Mock.Of(), corsService, - mockProvider.Object, loggerFactory, policyName: null); @@ -374,7 +372,7 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure httpContext.Request.Headers.Add(CorsConstants.Origin, new[] { "http://example.com" }); // Act - await middleware.Invoke(httpContext); + await middleware.Invoke(httpContext, mockProvider.Object); // Assert Assert.Equal(200, httpContext.Response.StatusCode);