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:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user