Skip to content

Conversation

@cincuranet
Copy link
Contributor

Fixes #53831.

@cincuranet cincuranet self-assigned this Jan 27, 2026
@cincuranet cincuranet added this to the 11.0.0 milestone Jan 27, 2026
@cincuranet cincuranet marked this pull request as ready for review January 27, 2026 08:58
@cincuranet cincuranet requested a review from a team January 27, 2026 08:59
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the XML documentation for the AddHostedService extension methods to clarify that these methods only register the service as IHostedService in the DI container, not as the concrete type itself. This addresses confusion reported in issue #53831 where developers expected the concrete type to also be registered.

Changes:

  • Added <remarks> sections to both overloads of AddHostedService explaining the registration behavior
  • Included code examples demonstrating the workaround pattern for registering both the concrete type and the hosted service

Comment on lines +28 to +29
/// services.AddSingleton&lt;SomeService&gt;();
/// services.AddHostedService(sp => sp.GetRequiredService&lt;SomeService&gt;());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggested solution will cause the service to be disposed twice when the service collection is disposed, correct? (assuming it implements IDisposable)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct.

But I don't see problem with that. The IDisposable docs clearly states:

If an object's Dispose method is called more than once, the object must ignore all calls after the first one. The object must not throw an exception if its Dispose method is called multiple times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AddHostedService does not register specified Type Parameter in DI container

3 participants