✅ AddHttpClient and AddHostedService
I have a hosted service that I register with my DI as so
and everything works fine. However I now need this service to use it's own
Everything resolved correctly and the program ran, except for the fact that the injected
I found changing my hosted service declaration to
serviceCollection.AddHostedService<ExampleService>()and everything works fine. However I now need this service to use it's own
HttpClient. That seemed like no issue, Just add serviceCollection.AddHttpClient<ExampleService>(x => x.Timeout = Timeout.InfiniteTimeSpan) and I should be good to go, or so I thought..Everything resolved correctly and the program ran, except for the fact that the injected
HttpClient did not in fact have infinite timeout. A quick breakpoint check later and i find that the configuration action isn't triggering, or from what I found online, that client isn't used at all and it's instead falling back to the standard HttpClient that AddHttpClient adds for everything that isn't the targeted type.I found changing my hosted service declaration to
serviceCollection.AddHostedService(provider => provider.GetRequiredService<ExampleService>()) resolves these issues but I'm honestly kinda at a loss as to why I had to do this and feel like I must have done something wrong to need to do this. Could anyone enlighten me on the cause of this?