Skip to content

Commit ccf3932

Browse files
authored
Update documentation for AddHostedService. (#123658)
Fixes #53831.
1 parent e322e12 commit ccf3932

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/ServiceCollectionHostedServiceExtensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ public static class ServiceCollectionHostedServiceExtensions
1616
/// <summary>
1717
/// Add an <see cref="IHostedService"/> registration for the given type.
1818
/// </summary>
19+
/// <remarks>
20+
/// Note that this creates registration for <see cref="IHostedService"/> specifically. Not for the actual <c>THostedService</c> type.
21+
/// If you want to register the actual type, you must do so separately.
22+
/// </remarks>
23+
/// <example>
24+
/// <para>
25+
/// The following code shows how to register a hosted service while also registering the actual <c>THostedService</c> type.
26+
/// </para>
27+
/// <code language="csharp">
28+
/// services.AddSingleton&lt;SomeService&gt;();
29+
/// services.AddHostedService(sp => sp.GetRequiredService&lt;SomeService&gt;());
30+
/// </code>
31+
/// </example>
1932
/// <typeparam name="THostedService">An <see cref="IHostedService"/> to register.</typeparam>
2033
/// <param name="services">The <see cref="IServiceCollection"/> to register with.</param>
2134
/// <returns>The original <see cref="IServiceCollection"/>.</returns>
@@ -30,6 +43,19 @@ public static class ServiceCollectionHostedServiceExtensions
3043
/// <summary>
3144
/// Add an <see cref="IHostedService"/> registration for the given type.
3245
/// </summary>
46+
/// <remarks>
47+
/// Note that this creates registration for <see cref="IHostedService"/> specifically. Not for the actual <c>THostedService</c> type.
48+
/// If you want to register the actual type, you must do so separately.
49+
/// </remarks>
50+
/// <example>
51+
/// <para>
52+
/// The following code shows how to register a hosted service while also registering the actual <c>THostedService</c> type.
53+
/// </para>
54+
/// <code language="csharp">
55+
/// services.AddSingleton&lt;SomeService&gt;(implementationFactory);
56+
/// services.AddHostedService(sp => sp.GetRequiredService&lt;SomeService&gt;());
57+
/// </code>
58+
/// </example>
3359
/// <typeparam name="THostedService">An <see cref="IHostedService"/> to register.</typeparam>
3460
/// <param name="services">The <see cref="IServiceCollection"/> to register with.</param>
3561
/// <param name="implementationFactory">A factory to create new instances of the service implementation.</param>

0 commit comments

Comments
 (0)