question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[.net 7] Inject HostedService into another HostedService?

See original GitHub issue

I have two Services: ServiceA and ServiceB. Both derive from BackgroundService and are added as such to the builder in Program.cs

var builder = WebApplication.CreateBuilder(args);
            var config = builder.Configuration;
            builder.Services.AddHostedService<ServiceA>();
            builder.Services.AddHostedService<ServiceB>();

ServiceB needs access to ServiceA instance, so that it can queue tasks that ServiceA needs to perform. I’m trying to inject ServiceA into ServiceB, but keep getting into issues.

  1. Tried constructor DI approach but it results in an exception at startup.

System.AggregateException: ‘Some services are not able to be constructed (Error while validating the service descriptor ‘ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: MyProject.ServiceB’: Unable to resolve service for type ‘MyProject.ServiceA’ while attempting to activate ‘MyProject.ServiceB’.)’

  1. Tried using IServiceProvider to GetService<ServiceA>() in constructor of ServiceB and also in ExecuteAsync method of ServiceB, but in both cases, GetService returns null.

While researching, I finally came upon this answer (read last line), which seems to suggest AddHostedService instances cannot be used in DI? https://stackoverflow.com/a/74376651/934257

Issue Analytics

  • State:open
  • Created 10 months ago
  • Reactions:1
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
AachoLoyacommented, Nov 14, 2022

Would be helpful, if this was added in Hosted Service or AddHostedService documentation. I had to spent hours to figure out this solution.

1reaction
gfoidlcommented, Nov 14, 2022

Out of interest: can you share more info why this design is needed? That coupling is IMO a bad choice – but I don’t know your requirements, so aplogize the “bad” (and I don’t try to push you in another direction again 😉). Can something like https://github.com/jbogard/MediatR work for it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

net core dependency injection to hosted service [duplicate]
I have chosen to implement IHostedService to run it in parallel with API. The hosted service needs some services injected.
Read more >
Background tasks with hosted services in ASP.NET Core
Learn how to implement background tasks with hosted services in ASP.NET Core. ... The scoped service can use dependency injection (DI).
Read more >
A Complete Guide to Hosted Service(s) in .NET 6 using C# 10
You can inject IHostApplicationLifetime into the Hosted Service constructor and register a callback function to listen to those events and take ...
Read more >
Using scoped services inside singletons - Sam Walpole
I recently came across this when trying to create a hosted service in ASP.NET Core. A hosted service allows you to create long...
Read more >
Why do we use IServiceProvider and not constructor for ...
You'd register IServiceFactory's implementation as a singleton, and then you can go back to using constructor injection in your hosted service.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found