Skip to content

AddHostedService does not register specified Type Parameter in DI container #53831

Description

@tthiery

Description

class FooService : IHostedService
{
    public FooService() {}
    /* ... */
}
class BarService : IHostedService
{
    public BarService(FooService foo) {}
    /* ... */
}

services.AddHostedService<FooService>(); // works
services.AddHostedService<BarService>(); // crashes when because DI container does not find FooService

The issue is quite straight forward in the .NET source code because the THostedService is not registered but instead a IHostedService ...

public static IServiceCollection AddHostedService<THostedService>(this IServiceCollection services)
    where THostedService : class, IHostedService
{
     services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, THostedService>());
     return services;
}

The common workaround seems to be like

services.AddSingleton<FooService>();
services.AddSingleton<BarService>(); 
services.AddHostedService(sp => sp.GetService<FooService>());
services.AddHostedService(sp => sp.GetService<BarService>());

My expectation was clearly, that the type I register with a ServiceCollection.Add* function later can be retrieved. I lost several hours due to this until I found the answer somewhere on StackOverflow.

Configuration

  • .NET 5
  • OS/Arch: Does not matter

Regression?

No

Other information

If the vote is not to double register the service (I assume the IHostedService is needed for the host logic), I would at least propose to add a warning in the documentation and maybe the function inline documentation.

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jun 7, 2021
  2. removed
    untriagedNew issue has not been triaged by the area owner
    on Jun 19, 2021
  3. added this to the 6.0.0 milestone on Jun 19, 2021
  4. modified the milestones: 6.0.0, Future on Jul 21, 2021
  5. tthiery commented on Jan 20, 2026

    @tthiery
    Author

    This is a human generated activity to keep it alive 😃

  6. karelz commented on Jan 21, 2026

    @karelz
    Member

    Triage: Given lower interest (2 people in 5 years) + there is simple workaround, we do not think we should add new API -- that is an overkill for a niche scenario.
    We could improve documentaiton. @cincuranet will look at it.

  7. added
    documentationDocumentation bug or enhancement, does not impact product or test code
    on Jan 21, 2026
  8. added
    in-prThere is an active PR which will close this issue when it is merged
    on Jan 27, 2026
  9. added a commit that references this issue on Jan 30, 2026
    ccf3932
  10. modified the milestones: Future, 11.0.0 on Feb 25, 2026
  11. added a commit that references this issue on Mar 2, 2026
    4b7e6f1
  12. locked and limited conversation to collaborators on Mar 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-Extensions-HostingdocumentationDocumentation bug or enhancement, does not impact product or test codein-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions