From ab0af2a291fb2d271eb052f41b66e4488af14095 Mon Sep 17 00:00:00 2001 From: DevAndersen <25183991+DevAndersen@users.noreply.github.com> Date: Thu, 7 Aug 2025 12:16:36 +0200 Subject: [PATCH] Changed methods in ServiceCollectionExtensions to return IServiceCollection instead of void (#19785) --- .../ServiceCollectionExtensions.cs | 47 +++++++++++++++---- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/src/Umbraco.Core/DependencyInjection/ServiceCollectionExtensions.cs b/src/Umbraco.Core/DependencyInjection/ServiceCollectionExtensions.cs index 579a34894a..555ce5208a 100644 --- a/src/Umbraco.Core/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/Umbraco.Core/DependencyInjection/ServiceCollectionExtensions.cs @@ -13,7 +13,10 @@ public static class ServiceCollectionExtensions /// /// Removes all previous registrations for the type . /// - public static void AddUnique( + /// + /// A reference to this instance after the operation has completed. + /// + public static IServiceCollection AddUnique( this IServiceCollection services) where TService : class where TImplementing : class, TService => @@ -26,7 +29,10 @@ public static class ServiceCollectionExtensions /// /// Removes all previous registrations for the type . /// - public static void AddUnique( + /// + /// A reference to this instance after the operation has completed. + /// + public static IServiceCollection AddUnique( this IServiceCollection services, ServiceLifetime lifetime) where TService : class @@ -34,6 +40,7 @@ public static class ServiceCollectionExtensions { services.RemoveAll(); services.Add(ServiceDescriptor.Describe(typeof(TService), typeof(TImplementing), lifetime)); + return services; } /// @@ -44,7 +51,10 @@ public static class ServiceCollectionExtensions /// Removes all previous registrations for the types & /// . /// - public static void AddMultipleUnique( + /// + /// A reference to this instance after the operation has completed. + /// + public static IServiceCollection AddMultipleUnique( this IServiceCollection services) where TService1 : class where TService2 : class @@ -57,7 +67,10 @@ public static class ServiceCollectionExtensions /// /// Removes all previous registrations for the types & . /// - public static void AddMultipleUnique( + /// + /// A reference to this instance after the operation has completed. + /// + public static IServiceCollection AddMultipleUnique( this IServiceCollection services, ServiceLifetime lifetime) where TService1 : class @@ -66,16 +79,20 @@ public static class ServiceCollectionExtensions { services.AddUnique(lifetime); services.AddUnique(factory => (TImplementing)factory.GetRequiredService(), lifetime); + return services; } - /// + /// /// Adds a service of type with an implementation factory method to the specified /// . /// /// /// Removes all previous registrations for the type . /// - public static void AddUnique( + /// + /// A reference to this instance after the operation has completed. + /// + public static IServiceCollection AddUnique( this IServiceCollection services, Func factory) where TService : class @@ -87,7 +104,10 @@ public static class ServiceCollectionExtensions /// /// Removes all previous registrations for the type . /// - public static void AddUnique( + /// + /// A reference to this instance after the operation has completed. + /// + public static IServiceCollection AddUnique( this IServiceCollection services, Func factory, ServiceLifetime lifetime) @@ -95,6 +115,7 @@ public static class ServiceCollectionExtensions { services.RemoveAll(); services.Add(ServiceDescriptor.Describe(typeof(TService), factory, lifetime)); + return services; } /// @@ -104,10 +125,14 @@ public static class ServiceCollectionExtensions /// /// Removes all previous registrations for the type specified by . /// - public static void AddUnique(this IServiceCollection services, Type serviceType, object instance) + /// + /// A reference to this instance after the operation has completed. + /// + public static IServiceCollection AddUnique(this IServiceCollection services, Type serviceType, object instance) { services.RemoveAll(serviceType); services.AddSingleton(serviceType, instance); + return services; } /// @@ -117,11 +142,15 @@ public static class ServiceCollectionExtensions /// /// Removes all previous registrations for the type type . /// - public static void AddUnique(this IServiceCollection services, TService instance) + /// + /// A reference to this instance after the operation has completed. + /// + public static IServiceCollection AddUnique(this IServiceCollection services, TService instance) where TService : class { services.RemoveAll(); services.AddSingleton(instance); + return services; } internal static IServiceCollection AddLazySupport(this IServiceCollection services)