Skip to content

Commit cb09035

Browse files
authored
Fix unguarded calls to ServiceDescriptor.ImplementationType for keyed services (#16604)
* Update integration test base class to verify that calls to ServiceDescriptor.ImplementationType are guarded for keyed services * Fix unguarded calls to ServiceDescriptor.ImplementationType for keyed services
1 parent de74ae4 commit cb09035

File tree

6 files changed

+11
-5
lines changed

6 files changed

+11
-5
lines changed

src/Umbraco.Cms.Api.Common/DependencyInjection/UmbracoBuilderApiExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static class UmbracoBuilderApiExtensions
1111
{
1212
public static IUmbracoBuilder AddUmbracoApiOpenApiUI(this IUmbracoBuilder builder)
1313
{
14-
if (builder.Services.Any(x => x.ImplementationType == typeof(OperationIdSelector)))
14+
if (builder.Services.Any(x => !x.IsKeyedService && x.ImplementationType == typeof(OperationIdSelector)))
1515
{
1616
return builder;
1717
}

src/Umbraco.Cms.Api.Common/DependencyInjection/UmbracoBuilderAuthExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static class UmbracoBuilderAuthExtensions
1717
{
1818
public static IUmbracoBuilder AddUmbracoOpenIddict(this IUmbracoBuilder builder)
1919
{
20-
if (builder.Services.Any(x=>x.ImplementationType == typeof(OpenIddictCleanupJob)) is false)
20+
if (builder.Services.Any(x => !x.IsKeyedService && x.ImplementationType == typeof(OpenIddictCleanupJob)) is false)
2121
{
2222
ConfigureOpenIddict(builder);
2323
}

src/Umbraco.Cms.Api.Management/DependencyInjection/UmbracoBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static IUmbracoBuilder AddUmbracoManagementApi(this IUmbracoBuilder build
2424
builder.Services.AddUnique<IConflictingRouteService, ConflictingRouteService>();
2525
builder.AddUmbracoApiOpenApiUI();
2626

27-
if (!services.Any(x => x.ImplementationType == typeof(JsonPatchService)))
27+
if (!services.Any(x => !x.IsKeyedService && x.ImplementationType == typeof(JsonPatchService)))
2828
{
2929
ModelsBuilderBuilderExtensions.AddModelsBuilder(builder)
3030
.AddJson()

tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ protected void ConfigureServices(IServiceCollection services)
136136

137137
services.AddLogger(webHostEnvironment, Configuration);
138138

139+
// Register a keyed service to verify that all calls to ServiceDescriptor.ImplementationType
140+
// are guarded by checking IsKeyedService first.
141+
// Failure to check this when accessing a keyed service descriptor's ImplementationType property
142+
// throws a InvalidOperationException.
143+
services.AddKeyedSingleton<object>("key");
144+
139145
// Add it!
140146
var hostingEnvironment = TestHelper.GetHostingEnvironment();
141147
var typeLoader = services.AddTypeLoader(

tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/LocksTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected void SetUp()
3636

3737
protected override void ConfigureTestServices(IServiceCollection services) =>
3838
// SQLite + retry policy makes tests fail, we retry before throwing distributed locking timeout.
39-
services.RemoveAll(x => x.ImplementationType == typeof(SqliteAddRetryPolicyInterceptor));
39+
services.RemoveAll(x => !x.IsKeyedService && x.ImplementationType == typeof(SqliteAddRetryPolicyInterceptor));
4040

4141
[Test]
4242
public void SingleReadLockTest()

tests/Umbraco.Tests.Integration/Umbraco.Persistence.EFCore/Scoping/EFCoreLockTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class EFCoreLockTests : UmbracoIntegrationTest
2323
protected override void ConfigureTestServices(IServiceCollection services)
2424
{
2525
// SQLite + retry policy makes tests fail, we retry before throwing distributed locking timeout.
26-
services.RemoveAll(x => x.ImplementationType == typeof(SqliteAddRetryPolicyInterceptor));
26+
services.RemoveAll(x => !x.IsKeyedService && x.ImplementationType == typeof(SqliteAddRetryPolicyInterceptor));
2727

2828
// Remove all locking implementations to ensure we only use EFCoreDistributedLockingMechanisms
2929
services.RemoveAll(x => x.ServiceType == typeof(IDistributedLockingMechanism));

0 commit comments

Comments
 (0)