Use ApplicationPartFactory when adding application parts

Fixes https://github.com/aspnet/AspNetCore/issues/8288
This commit is contained in:
Pranav K 2019-03-07 10:19:01 -08:00
parent 8aa02180fb
commit 41e6fc8ab0
6 changed files with 92 additions and 7 deletions

View File

@ -3,7 +3,7 @@
"devDependencies": {
"jest": "^23.6.0",
"merge": "^1.2.1",
"puppeteer": "^1.12.2"
"puppeteer": "^1.13.0"
},
"dependencies": {},
"scripts": {

View File

@ -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"

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<IServiceCollection>(), 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<ApplicationPart>();
public override IEnumerable<ApplicationPart> GetApplicationParts(Assembly assembly)
{
yield return TestPart;
}
}
}
}

View File

@ -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<IServiceCollection>(), 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<ApplicationPart>();
public override IEnumerable<ApplicationPart> GetApplicationParts(Assembly assembly)
{
yield return TestPart;
}
}
}
}