diff --git a/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.Collections.cs b/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.Collections.cs index bc83695d94..609c5305dc 100644 --- a/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.Collections.cs +++ b/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.Collections.cs @@ -2,6 +2,7 @@ using Umbraco.Cms.Core.DependencyInjection; using Umbraco.Cms.Core.Packaging; using Umbraco.Cms.Infrastructure.Persistence; using Umbraco.Cms.Infrastructure.Persistence.Mappers; +using Umbraco.Cms.Infrastructure.Runtime; namespace Umbraco.Extensions; @@ -17,6 +18,10 @@ public static partial class UmbracoBuilderExtensions public static MapperCollectionBuilder? Mappers(this IUmbracoBuilder builder) => builder.WithCollectionBuilder(); + /// + /// Gets the NPoco mappers collection builder. + /// + /// The builder. public static NPocoMapperCollectionBuilder? NPocoMappers(this IUmbracoBuilder builder) => builder.WithCollectionBuilder(); @@ -26,4 +31,11 @@ public static partial class UmbracoBuilderExtensions /// The builder. public static PackageMigrationPlanCollectionBuilder? PackageMigrationPlans(this IUmbracoBuilder builder) => builder.WithCollectionBuilder(); + + /// + /// Gets the runtime mode validators collection builder. + /// + /// The builder. + public static RuntimeModeValidatorCollectionBuilder RuntimeModeValidators(this IUmbracoBuilder builder) + => builder.WithCollectionBuilder(); } diff --git a/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs b/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs index 154bae9cd0..62bafcd28e 100644 --- a/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs +++ b/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs @@ -86,11 +86,12 @@ public static partial class UmbracoBuilderExtensions // Add runtime mode validation builder.Services.AddSingleton(); - builder.Services.AddTransient(); - builder.Services.AddTransient(); - builder.Services.AddTransient(); - builder.Services.AddTransient(); - builder.Services.AddTransient(); + builder.RuntimeModeValidators() + .Add() + .Add() + .Add() + .Add() + .Add(); // composers builder diff --git a/src/Umbraco.Infrastructure/Runtime/RuntimeModeValidationService.cs b/src/Umbraco.Infrastructure/Runtime/RuntimeModeValidationService.cs index 85eec91786..c4bbeb6902 100644 --- a/src/Umbraco.Infrastructure/Runtime/RuntimeModeValidationService.cs +++ b/src/Umbraco.Infrastructure/Runtime/RuntimeModeValidationService.cs @@ -29,11 +29,18 @@ internal class RuntimeModeValidationService : IRuntimeModeValidationService var validationMessages = new List(); // Runtime mode validators are registered transient, but this service is registered as singleton - foreach (var runtimeModeValidator in _serviceProvider.GetServices()) + using (var scope = _serviceProvider.CreateScope()) { - if (runtimeModeValidator.Validate(runtimeMode, out var validationMessage) == false) + var runtimeModeValidators = scope.ServiceProvider.GetService(); + if (runtimeModeValidators is not null) { - validationMessages.Add(validationMessage); + foreach (var runtimeModeValidator in runtimeModeValidators) + { + if (runtimeModeValidator.Validate(runtimeMode, out var validationMessage) == false) + { + validationMessages.Add(validationMessage); + } + } } } diff --git a/src/Umbraco.Infrastructure/Runtime/RuntimeModeValidatorCollection.cs b/src/Umbraco.Infrastructure/Runtime/RuntimeModeValidatorCollection.cs new file mode 100644 index 0000000000..97f25f3b8f --- /dev/null +++ b/src/Umbraco.Infrastructure/Runtime/RuntimeModeValidatorCollection.cs @@ -0,0 +1,10 @@ +using Umbraco.Cms.Core.Composing; + +namespace Umbraco.Cms.Infrastructure.Runtime; + +public class RuntimeModeValidatorCollection : BuilderCollectionBase +{ + public RuntimeModeValidatorCollection(Func> items) + : base(items) + { } +} diff --git a/src/Umbraco.Infrastructure/Runtime/RuntimeModeValidatorCollectionBuilder.cs b/src/Umbraco.Infrastructure/Runtime/RuntimeModeValidatorCollectionBuilder.cs new file mode 100644 index 0000000000..4c755a6040 --- /dev/null +++ b/src/Umbraco.Infrastructure/Runtime/RuntimeModeValidatorCollectionBuilder.cs @@ -0,0 +1,11 @@ +using Microsoft.Extensions.DependencyInjection; +using Umbraco.Cms.Core.Composing; + +namespace Umbraco.Cms.Infrastructure.Runtime; + +public class RuntimeModeValidatorCollectionBuilder : SetCollectionBuilderBase +{ + protected override ServiceLifetime CollectionLifetime => ServiceLifetime.Transient; + + protected override RuntimeModeValidatorCollectionBuilder This => this; +}