From 41e6fc8ab03e6b93dafbfdc5474cc74523c571db Mon Sep 17 00:00:00 2001 From: Pranav K Date: Thu, 7 Mar 2019 10:19:01 -0800 Subject: [PATCH] Use ApplicationPartFactory when adding application parts Fixes https://github.com/aspnet/AspNetCore/issues/8288 --- .../CORS/test/FunctionalTests/package.json | 2 +- .../CORS/test/FunctionalTests/yarn.lock | 8 ++--- .../MvcCoreMvcBuilderExtensions.cs | 9 ++++- .../MvcCoreMvcCoreBuilderExtensions.cs | 9 ++++- .../MvcBuilderExtensionsTest.cs | 35 ++++++++++++++++++ .../MvcCoreBuilderExtensionsTest.cs | 36 +++++++++++++++++++ 6 files changed, 92 insertions(+), 7 deletions(-) diff --git a/src/Middleware/CORS/test/FunctionalTests/package.json b/src/Middleware/CORS/test/FunctionalTests/package.json index 5c179dd000..a2ec721939 100644 --- a/src/Middleware/CORS/test/FunctionalTests/package.json +++ b/src/Middleware/CORS/test/FunctionalTests/package.json @@ -3,7 +3,7 @@ "devDependencies": { "jest": "^23.6.0", "merge": "^1.2.1", - "puppeteer": "^1.12.2" + "puppeteer": "^1.13.0" }, "dependencies": {}, "scripts": { diff --git a/src/Middleware/CORS/test/FunctionalTests/yarn.lock b/src/Middleware/CORS/test/FunctionalTests/yarn.lock index acdd7e7bd4..e9b38ac65c 100644 --- a/src/Middleware/CORS/test/FunctionalTests/yarn.lock +++ b/src/Middleware/CORS/test/FunctionalTests/yarn.lock @@ -2826,10 +2826,10 @@ punycode@^2.1.0: resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec" integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A== -puppeteer@^1.12.2: - version "1.12.2" - resolved "https://registry.yarnpkg.com/puppeteer/-/puppeteer-1.12.2.tgz#dbc36afc3ba2d7182b1a37523c0081a0e8507c9a" - integrity sha512-xWSyCeD6EazGlfnQweMpM+Hs6X6PhUYhNTHKFj/axNZDq4OmrVERf70isBf7HsnFgB3zOC1+23/8+wCAZYg+Pg== +puppeteer@^1.13.0: + version "1.13.0" + resolved "https://registry.yarnpkg.com/puppeteer/-/puppeteer-1.13.0.tgz#187ccf5ed5caf08ed1291b262d033cc364bf88ab" + integrity sha512-LUXgvhjfB/P6IOUDAKxOcbCz9ISwBLL9UpKghYrcBDwrOGx1m60y0iN2M64mdAUbT4+7oZM5DTxOW7equa2fxQ== dependencies: debug "^4.1.0" extract-zip "^1.6.6" diff --git a/src/Mvc/Mvc.Core/src/DependencyInjection/MvcCoreMvcBuilderExtensions.cs b/src/Mvc/Mvc.Core/src/DependencyInjection/MvcCoreMvcBuilderExtensions.cs index c2640043c2..74cbeadb21 100644 --- a/src/Mvc/Mvc.Core/src/DependencyInjection/MvcCoreMvcBuilderExtensions.cs +++ b/src/Mvc/Mvc.Core/src/DependencyInjection/MvcCoreMvcBuilderExtensions.cs @@ -85,7 +85,14 @@ namespace Microsoft.Extensions.DependencyInjection throw new ArgumentNullException(nameof(assembly)); } - builder.ConfigureApplicationPartManager(manager => manager.ApplicationParts.Add(new AssemblyPart(assembly))); + builder.ConfigureApplicationPartManager(manager => + { + var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly); + foreach (var applicationPart in partFactory.GetApplicationParts(assembly)) + { + manager.ApplicationParts.Add(applicationPart); + } + }); return builder; } diff --git a/src/Mvc/Mvc.Core/src/DependencyInjection/MvcCoreMvcCoreBuilderExtensions.cs b/src/Mvc/Mvc.Core/src/DependencyInjection/MvcCoreMvcCoreBuilderExtensions.cs index 1e1aef24ba..c87870a6e3 100644 --- a/src/Mvc/Mvc.Core/src/DependencyInjection/MvcCoreMvcCoreBuilderExtensions.cs +++ b/src/Mvc/Mvc.Core/src/DependencyInjection/MvcCoreMvcCoreBuilderExtensions.cs @@ -162,7 +162,14 @@ namespace Microsoft.Extensions.DependencyInjection throw new ArgumentNullException(nameof(assembly)); } - builder.ConfigureApplicationPartManager(manager => manager.ApplicationParts.Add(new AssemblyPart(assembly))); + builder.ConfigureApplicationPartManager(manager => + { + var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly); + foreach (var applicationPart in partFactory.GetApplicationParts(assembly)) + { + manager.ApplicationParts.Add(applicationPart); + } + }); return builder; } diff --git a/src/Mvc/Mvc.Core/test/DependencyInjection/MvcBuilderExtensionsTest.cs b/src/Mvc/Mvc.Core/test/DependencyInjection/MvcBuilderExtensionsTest.cs index 6d7fd73b51..5f24f5644d 100644 --- a/src/Mvc/Mvc.Core/test/DependencyInjection/MvcBuilderExtensionsTest.cs +++ b/src/Mvc/Mvc.Core/test/DependencyInjection/MvcBuilderExtensionsTest.cs @@ -1,8 +1,11 @@ // 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 System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Reflection.Emit; using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.MvcServiceCollectionExtensionsTestControllers; @@ -34,6 +37,28 @@ namespace Microsoft.AspNetCore.Mvc Assert.Equal(assembly, assemblyPart.Assembly); } + [Fact] + public void AddApplicationPart_UsesPartFactory_ToRetrieveApplicationParts() + { + // Arrange + var manager = new ApplicationPartManager(); + var builder = new MvcBuilder(Mock.Of(), manager); + var assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.Run); + + var attribute = new CustomAttributeBuilder(typeof(ProvideApplicationPartFactoryAttribute).GetConstructor( + new[] { typeof(Type) }), + new[] { typeof(TestApplicationPartFactory) }); + + assembly.SetCustomAttribute(attribute); + + // Act + builder.AddApplicationPart(assembly); + + // Assert + var part = Assert.Single(builder.PartManager.ApplicationParts); + Assert.Same(TestApplicationPartFactory.TestPart, part); + } + [Fact] public void ConfigureApplicationParts_InvokesSetupAction() { @@ -152,6 +177,16 @@ namespace Microsoft.AspNetCore.Mvc return manager; } + + private class TestApplicationPartFactory : ApplicationPartFactory + { + public static readonly ApplicationPart TestPart = Mock.Of(); + + public override IEnumerable GetApplicationParts(Assembly assembly) + { + yield return TestPart; + } + } } } diff --git a/src/Mvc/Mvc.Core/test/DependencyInjection/MvcCoreBuilderExtensionsTest.cs b/src/Mvc/Mvc.Core/test/DependencyInjection/MvcCoreBuilderExtensionsTest.cs index 1f37d86316..0fb59492d6 100644 --- a/src/Mvc/Mvc.Core/test/DependencyInjection/MvcCoreBuilderExtensionsTest.cs +++ b/src/Mvc/Mvc.Core/test/DependencyInjection/MvcCoreBuilderExtensionsTest.cs @@ -1,8 +1,11 @@ // 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 System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Reflection.Emit; using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; @@ -31,6 +34,28 @@ namespace Microsoft.AspNetCore.Mvc.DependencyInjection Assert.Equal(assembly, assemblyPart.Assembly); } + [Fact] + public void AddApplicationPart_UsesPartFactory_ToRetrieveApplicationParts() + { + // Arrange + var manager = new ApplicationPartManager(); + var builder = new MvcCoreBuilder(Mock.Of(), manager); + var assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.Run); + + var attribute = new CustomAttributeBuilder(typeof(ProvideApplicationPartFactoryAttribute).GetConstructor( + new[] { typeof(Type) }), + new[] { typeof(TestApplicationPartFactory) }); + + assembly.SetCustomAttribute(attribute); + + // Act + builder.AddApplicationPart(assembly); + + // Assert + var part = Assert.Single(builder.PartManager.ApplicationParts); + Assert.Same(TestApplicationPartFactory.TestPart, part); + } + [Fact] public void ConfigureApplicationParts_InvokesSetupAction() { @@ -78,5 +103,16 @@ namespace Microsoft.AspNetCore.Mvc.DependencyInjection .Value; Assert.True(options.SuppressMapClientErrors); } + + + private class TestApplicationPartFactory : ApplicationPartFactory + { + public static readonly ApplicationPart TestPart = Mock.Of(); + + public override IEnumerable GetApplicationParts(Assembly assembly) + { + yield return TestPart; + } + } } }