Skip to content

Commit 7aa6a13

Browse files
[release/2.2] Correct mapping of raw startup classes to their respective features (#18430)
--------- Co-authored-by: Georg von Kries <[email protected]>
1 parent dbf0123 commit 7aa6a13

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src/OrchardCore/OrchardCore/Shell/Builders/ShellContainerFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ public async Task<IServiceProvider> CreateContainerAsync(ShellSettings settings,
115115
// Let any module add custom service descriptors to the tenant.
116116
foreach (var startup in startups)
117117
{
118-
var feature = blueprint.Dependencies.FirstOrDefault(x => x.Key == startup.GetType()).Value?.FirstOrDefault();
118+
// For raw startup classes, use the original type named 'Startup' to find the feature.
119+
var startupType = startup is StartupBaseMock mock ? mock.StartupType : startup.GetType();
120+
var feature = blueprint.Dependencies.FirstOrDefault(x => x.Key == startupType).Value?.FirstOrDefault();
119121

120122
// If the startup is not coming from an extension, associate it to the application feature.
121123
// For instance when Startup classes are registered with Configure<Startup>() from the application.

src/OrchardCore/OrchardCore/Shell/Builders/StartupBaseMock.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public StartupBaseMock(
3636
/// <inheritdoc />
3737
public override int ConfigureOrder { get; }
3838

39+
internal Type StartupType => _startup.GetType();
40+
3941
public override void ConfigureServices(IServiceCollection services)
4042
{
4143
if (_configureService == null)

test/OrchardCore.Tests/Shell/ShellContainerFactoryTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,24 @@ public async Task AssignsTypeToMultipleFeatures()
169169
Assert.Equal(expectedFeatureInfos, typeFeatureProvider.GetFeaturesForDependency(typeof(TestService)));
170170
}
171171

172+
[Fact]
173+
public async Task RawStartupIsAssignedToCorrectFeature()
174+
{
175+
var shellBlueprint = CreateBlueprint();
176+
177+
var expectedFeatureInfo = AddStartup(shellBlueprint, typeof(Startup));
178+
179+
var container = (await _shellContainerFactory
180+
.CreateContainerAsync(_uninitializedDefaultShell, shellBlueprint))
181+
.CreateScope()
182+
.ServiceProvider;
183+
184+
var typeFeatureProvider = _applicationServiceProvider.GetService<ITypeFeatureProvider>();
185+
186+
Assert.IsType<TestService>(container.GetRequiredService(typeof(ITestService)));
187+
Assert.Same(expectedFeatureInfo, typeFeatureProvider.GetFeatureForDependency(typeof(TestService)));
188+
}
189+
172190
private static ShellBlueprint CreateBlueprint()
173191
{
174192
return new ShellBlueprint
@@ -280,4 +298,15 @@ public override void ConfigureServices(IServiceCollection services)
280298
services.AddScoped<ITwoHostSingletonsOfTheSameType, ShellScopedOfTheSametype>();
281299
}
282300
}
301+
302+
// A raw startup class that is not derived from StartupBase. Must be named Startup.
303+
private sealed class Startup
304+
{
305+
#pragma warning disable CA1822 // Mark members as static
306+
public void ConfigureServices(IServiceCollection services)
307+
#pragma warning restore CA1822 // Mark members as static
308+
{
309+
services.AddScoped<ITestService, TestService>();
310+
}
311+
}
283312
}

test/OrchardCore.Tests/Stubs/StubExtensionManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public IEnumerable<IFeatureInfo> GetFeatureDependencies(string featureId)
2727

2828
public IEnumerable<IFeatureInfo> GetFeatures()
2929
{
30-
return [];
30+
return [new FeatureInfo(GetType().Assembly.GetName().Name, new ExtensionInfo(GetType().Assembly.GetName().Name))];
3131
}
3232

3333
public IEnumerable<IFeatureInfo> GetFeatures(string[] featureIdsToLoad)

0 commit comments

Comments
 (0)