From c4fc28db7ce0dd4c4e41da68201b8697f93ffbe2 Mon Sep 17 00:00:00 2001 From: Jiri Cincura Date: Tue, 27 Jan 2026 09:19:20 +0100 Subject: [PATCH 1/3] Update documentation for AddHostedService. --- ...erviceCollectionHostedServiceExtensions.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/ServiceCollectionHostedServiceExtensions.cs b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/ServiceCollectionHostedServiceExtensions.cs index 67ea8b13cbd58c..8ba9ae09143970 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/ServiceCollectionHostedServiceExtensions.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/ServiceCollectionHostedServiceExtensions.cs @@ -16,6 +16,19 @@ public static class ServiceCollectionHostedServiceExtensions /// /// Add an registration for the given type. /// + /// + /// + /// Note that this creates registration for specifically. Not for the actual THostedService type. + /// + /// + /// If you want to register the actual type, you must do so separately. + /// For example: + /// + /// services.AddSingleton<SomeService>(); + /// services.AddHostedService(sp => sp.GetService<SomeService>()); + /// + /// + /// /// An to register. /// The to register with. /// The original . @@ -30,6 +43,19 @@ public static class ServiceCollectionHostedServiceExtensions /// /// Add an registration for the given type. /// + /// + /// + /// Note that this creates registration for specifically. Not for the actual THostedService type. + /// + /// + /// If you want to register the actual type, you must do so separately. + /// For example: + /// + /// services.AddSingleton<SomeService>(implementationFactory); + /// services.AddHostedService(sp => sp.GetService<SomeService>()); + /// + /// + /// /// An to register. /// The to register with. /// A factory to create new instances of the service implementation. From 08fa903370bf23c4499c4975d0e6d85f7f2b9420 Mon Sep 17 00:00:00 2001 From: Jiri Cincura Date: Tue, 27 Jan 2026 10:11:52 +0100 Subject: [PATCH 2/3] Copilot feedback. --- ...erviceCollectionHostedServiceExtensions.cs | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/ServiceCollectionHostedServiceExtensions.cs b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/ServiceCollectionHostedServiceExtensions.cs index 8ba9ae09143970..7ee4382ce84446 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/ServiceCollectionHostedServiceExtensions.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/ServiceCollectionHostedServiceExtensions.cs @@ -17,18 +17,15 @@ public static class ServiceCollectionHostedServiceExtensions /// Add an registration for the given type. /// /// - /// /// Note that this creates registration for specifically. Not for the actual THostedService type. - /// - /// /// If you want to register the actual type, you must do so separately. - /// For example: - /// + /// + /// + /// /// services.AddSingleton<SomeService>(); - /// services.AddHostedService(sp => sp.GetService<SomeService>()); + /// services.AddHostedService(sp => sp.GetRequiredService<SomeService>()); /// - /// - /// + /// /// An to register. /// The to register with. /// The original . @@ -44,18 +41,15 @@ public static class ServiceCollectionHostedServiceExtensions /// Add an registration for the given type. /// /// - /// /// Note that this creates registration for specifically. Not for the actual THostedService type. - /// - /// /// If you want to register the actual type, you must do so separately. - /// For example: - /// + /// + /// + /// /// services.AddSingleton<SomeService>(implementationFactory); - /// services.AddHostedService(sp => sp.GetService<SomeService>()); + /// services.AddHostedService(sp => sp.GetRequiredService<SomeService>()); /// - /// - /// + /// /// An to register. /// The to register with. /// A factory to create new instances of the service implementation. From a92410bc0e27a853a23e3d5b6794f78a62138dff Mon Sep 17 00:00:00 2001 From: Jiri Cincura Date: Tue, 27 Jan 2026 10:22:04 +0100 Subject: [PATCH 3/3] Improve docs. --- .../src/ServiceCollectionHostedServiceExtensions.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/ServiceCollectionHostedServiceExtensions.cs b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/ServiceCollectionHostedServiceExtensions.cs index 7ee4382ce84446..6e57df438308b3 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/ServiceCollectionHostedServiceExtensions.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/ServiceCollectionHostedServiceExtensions.cs @@ -21,6 +21,9 @@ public static class ServiceCollectionHostedServiceExtensions /// If you want to register the actual type, you must do so separately. /// /// + /// + /// The following code shows how to register a hosted service while also registering the actual THostedService type. + /// /// /// services.AddSingleton<SomeService>(); /// services.AddHostedService(sp => sp.GetRequiredService<SomeService>()); @@ -45,6 +48,9 @@ public static class ServiceCollectionHostedServiceExtensions /// If you want to register the actual type, you must do so separately. /// /// + /// + /// The following code shows how to register a hosted service while also registering the actual THostedService type. + /// /// /// services.AddSingleton<SomeService>(implementationFactory); /// services.AddHostedService(sp => sp.GetRequiredService<SomeService>());