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
This commit is contained in:
Josh Brown
2024-06-22 10:20:12 +01:00
committed by GitHub
parent de74ae4c04
commit cb090353f4
6 changed files with 11 additions and 5 deletions

View File

@@ -11,7 +11,7 @@ public static class UmbracoBuilderApiExtensions
{
public static IUmbracoBuilder AddUmbracoApiOpenApiUI(this IUmbracoBuilder builder)
{
if (builder.Services.Any(x => x.ImplementationType == typeof(OperationIdSelector)))
if (builder.Services.Any(x => !x.IsKeyedService && x.ImplementationType == typeof(OperationIdSelector)))
{
return builder;
}

View File

@@ -17,7 +17,7 @@ public static class UmbracoBuilderAuthExtensions
{
public static IUmbracoBuilder AddUmbracoOpenIddict(this IUmbracoBuilder builder)
{
if (builder.Services.Any(x=>x.ImplementationType == typeof(OpenIddictCleanupJob)) is false)
if (builder.Services.Any(x => !x.IsKeyedService && x.ImplementationType == typeof(OpenIddictCleanupJob)) is false)
{
ConfigureOpenIddict(builder);
}

View File

@@ -24,7 +24,7 @@ public static partial class UmbracoBuilderExtensions
builder.Services.AddUnique<IConflictingRouteService, ConflictingRouteService>();
builder.AddUmbracoApiOpenApiUI();
if (!services.Any(x => x.ImplementationType == typeof(JsonPatchService)))
if (!services.Any(x => !x.IsKeyedService && x.ImplementationType == typeof(JsonPatchService)))
{
ModelsBuilderBuilderExtensions.AddModelsBuilder(builder)
.AddJson()

View File

@@ -136,6 +136,12 @@ public abstract class UmbracoIntegrationTest : UmbracoIntegrationTestBase
services.AddLogger(webHostEnvironment, Configuration);
// Register a keyed service to verify that all calls to ServiceDescriptor.ImplementationType
// are guarded by checking IsKeyedService first.
// Failure to check this when accessing a keyed service descriptor's ImplementationType property
// throws a InvalidOperationException.
services.AddKeyedSingleton<object>("key");
// Add it!
var hostingEnvironment = TestHelper.GetHostingEnvironment();
var typeLoader = services.AddTypeLoader(

View File

@@ -36,7 +36,7 @@ public class LocksTests : UmbracoIntegrationTest
protected override void ConfigureTestServices(IServiceCollection services) =>
// SQLite + retry policy makes tests fail, we retry before throwing distributed locking timeout.
services.RemoveAll(x => x.ImplementationType == typeof(SqliteAddRetryPolicyInterceptor));
services.RemoveAll(x => !x.IsKeyedService && x.ImplementationType == typeof(SqliteAddRetryPolicyInterceptor));
[Test]
public void SingleReadLockTest()

View File

@@ -23,7 +23,7 @@ public class EFCoreLockTests : UmbracoIntegrationTest
protected override void ConfigureTestServices(IServiceCollection services)
{
// SQLite + retry policy makes tests fail, we retry before throwing distributed locking timeout.
services.RemoveAll(x => x.ImplementationType == typeof(SqliteAddRetryPolicyInterceptor));
services.RemoveAll(x => !x.IsKeyedService && x.ImplementationType == typeof(SqliteAddRetryPolicyInterceptor));
// Remove all locking implementations to ensure we only use EFCoreDistributedLockingMechanisms
services.RemoveAll(x => x.ServiceType == typeof(IDistributedLockingMechanism));