Skip to content

Commit 41e6fc8

Browse files
committed
Use ApplicationPartFactory when adding application parts
Fixes #8288
1 parent 8aa0218 commit 41e6fc8

File tree

6 files changed

+92
-7
lines changed

6 files changed

+92
-7
lines changed

src/Middleware/CORS/test/FunctionalTests/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"devDependencies": {
44
"jest": "^23.6.0",
55
"merge": "^1.2.1",
6-
"puppeteer": "^1.12.2"
6+
"puppeteer": "^1.13.0"
77
},
88
"dependencies": {},
99
"scripts": {

src/Middleware/CORS/test/FunctionalTests/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2826,10 +2826,10 @@ punycode@^2.1.0:
28262826
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
28272827
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==
28282828

2829-
puppeteer@^1.12.2:
2830-
version "1.12.2"
2831-
resolved "https://registry.yarnpkg.com/puppeteer/-/puppeteer-1.12.2.tgz#dbc36afc3ba2d7182b1a37523c0081a0e8507c9a"
2832-
integrity sha512-xWSyCeD6EazGlfnQweMpM+Hs6X6PhUYhNTHKFj/axNZDq4OmrVERf70isBf7HsnFgB3zOC1+23/8+wCAZYg+Pg==
2829+
puppeteer@^1.13.0:
2830+
version "1.13.0"
2831+
resolved "https://registry.yarnpkg.com/puppeteer/-/puppeteer-1.13.0.tgz#187ccf5ed5caf08ed1291b262d033cc364bf88ab"
2832+
integrity sha512-LUXgvhjfB/P6IOUDAKxOcbCz9ISwBLL9UpKghYrcBDwrOGx1m60y0iN2M64mdAUbT4+7oZM5DTxOW7equa2fxQ==
28332833
dependencies:
28342834
debug "^4.1.0"
28352835
extract-zip "^1.6.6"

src/Mvc/Mvc.Core/src/DependencyInjection/MvcCoreMvcBuilderExtensions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,14 @@ public static IMvcBuilder AddApplicationPart(this IMvcBuilder builder, Assembly
8585
throw new ArgumentNullException(nameof(assembly));
8686
}
8787

88-
builder.ConfigureApplicationPartManager(manager => manager.ApplicationParts.Add(new AssemblyPart(assembly)));
88+
builder.ConfigureApplicationPartManager(manager =>
89+
{
90+
var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
91+
foreach (var applicationPart in partFactory.GetApplicationParts(assembly))
92+
{
93+
manager.ApplicationParts.Add(applicationPart);
94+
}
95+
});
8996

9097
return builder;
9198
}

src/Mvc/Mvc.Core/src/DependencyInjection/MvcCoreMvcCoreBuilderExtensions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,14 @@ public static IMvcCoreBuilder AddApplicationPart(this IMvcCoreBuilder builder, A
162162
throw new ArgumentNullException(nameof(assembly));
163163
}
164164

165-
builder.ConfigureApplicationPartManager(manager => manager.ApplicationParts.Add(new AssemblyPart(assembly)));
165+
builder.ConfigureApplicationPartManager(manager =>
166+
{
167+
var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
168+
foreach (var applicationPart in partFactory.GetApplicationParts(assembly))
169+
{
170+
manager.ApplicationParts.Add(applicationPart);
171+
}
172+
});
166173

167174
return builder;
168175
}

src/Mvc/Mvc.Core/test/DependencyInjection/MvcBuilderExtensionsTest.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
5+
using System.Collections.Generic;
46
using System.Linq;
57
using System.Reflection;
8+
using System.Reflection.Emit;
69
using Microsoft.AspNetCore.Mvc.ApplicationParts;
710
using Microsoft.AspNetCore.Mvc.Controllers;
811
using Microsoft.AspNetCore.Mvc.MvcServiceCollectionExtensionsTestControllers;
@@ -34,6 +37,28 @@ public void AddApplicationPart_AddsAnApplicationPart_ToTheListOfPartsOnTheBuilde
3437
Assert.Equal(assembly, assemblyPart.Assembly);
3538
}
3639

40+
[Fact]
41+
public void AddApplicationPart_UsesPartFactory_ToRetrieveApplicationParts()
42+
{
43+
// Arrange
44+
var manager = new ApplicationPartManager();
45+
var builder = new MvcBuilder(Mock.Of<IServiceCollection>(), manager);
46+
var assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.Run);
47+
48+
var attribute = new CustomAttributeBuilder(typeof(ProvideApplicationPartFactoryAttribute).GetConstructor(
49+
new[] { typeof(Type) }),
50+
new[] { typeof(TestApplicationPartFactory) });
51+
52+
assembly.SetCustomAttribute(attribute);
53+
54+
// Act
55+
builder.AddApplicationPart(assembly);
56+
57+
// Assert
58+
var part = Assert.Single(builder.PartManager.ApplicationParts);
59+
Assert.Same(TestApplicationPartFactory.TestPart, part);
60+
}
61+
3762
[Fact]
3863
public void ConfigureApplicationParts_InvokesSetupAction()
3964
{
@@ -152,6 +177,16 @@ private static ApplicationPartManager GetApplicationPartManager(params TypeInfo[
152177

153178
return manager;
154179
}
180+
181+
private class TestApplicationPartFactory : ApplicationPartFactory
182+
{
183+
public static readonly ApplicationPart TestPart = Mock.Of<ApplicationPart>();
184+
185+
public override IEnumerable<ApplicationPart> GetApplicationParts(Assembly assembly)
186+
{
187+
yield return TestPart;
188+
}
189+
}
155190
}
156191
}
157192

src/Mvc/Mvc.Core/test/DependencyInjection/MvcCoreBuilderExtensionsTest.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
5+
using System.Collections.Generic;
46
using System.Linq;
57
using System.Reflection;
8+
using System.Reflection.Emit;
69
using Microsoft.AspNetCore.Mvc.ApplicationParts;
710
using Microsoft.Extensions.DependencyInjection;
811
using Microsoft.Extensions.Options;
@@ -31,6 +34,28 @@ public void AddApplicationPart_AddsAnApplicationPart_ToTheListOfPartsOnTheBuilde
3134
Assert.Equal(assembly, assemblyPart.Assembly);
3235
}
3336

37+
[Fact]
38+
public void AddApplicationPart_UsesPartFactory_ToRetrieveApplicationParts()
39+
{
40+
// Arrange
41+
var manager = new ApplicationPartManager();
42+
var builder = new MvcCoreBuilder(Mock.Of<IServiceCollection>(), manager);
43+
var assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.Run);
44+
45+
var attribute = new CustomAttributeBuilder(typeof(ProvideApplicationPartFactoryAttribute).GetConstructor(
46+
new[] { typeof(Type) }),
47+
new[] { typeof(TestApplicationPartFactory) });
48+
49+
assembly.SetCustomAttribute(attribute);
50+
51+
// Act
52+
builder.AddApplicationPart(assembly);
53+
54+
// Assert
55+
var part = Assert.Single(builder.PartManager.ApplicationParts);
56+
Assert.Same(TestApplicationPartFactory.TestPart, part);
57+
}
58+
3459
[Fact]
3560
public void ConfigureApplicationParts_InvokesSetupAction()
3661
{
@@ -78,5 +103,16 @@ public void ConfigureApiBehaviorOptions_InvokesSetupAction()
78103
.Value;
79104
Assert.True(options.SuppressMapClientErrors);
80105
}
106+
107+
108+
private class TestApplicationPartFactory : ApplicationPartFactory
109+
{
110+
public static readonly ApplicationPart TestPart = Mock.Of<ApplicationPart>();
111+
112+
public override IEnumerable<ApplicationPart> GetApplicationParts(Assembly assembly)
113+
{
114+
yield return TestPart;
115+
}
116+
}
81117
}
82118
}

0 commit comments

Comments
 (0)