Closed aeos closed 5 years ago
@rynowak
services.AddHttpClient
( c => { ... })
If you're doing this then you should be resolving PCSyncService
from DI.
I think AddHostedService adds the service as a transient dependency. Without the AddHttpClient extension call my service receives the Ilogger through DI
If anyone has a similar issue in the future, I figured out how to solve the issue, but I do not know why. I could not find the appropriate pattern for passing the HttpClient directly into an IHostedService. You must create another service interface, and pass that into the IHostedService. You must define this new Interface service first, then the HttpClient definition, then the HostedService binding.
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IPeopleService, PeopleService>();
services.AddHttpClient<IPeopleService,PeopleService>( c => {
// configuration removed
});
services.AddHostedService<PCSyncService>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
Hi,
I have a functional IHostedService that relies on an HttpClient, When I have the client defined in the service, everything works. However, if I use dependency injection like the code below, I get the error that follows. I am not quite sure what I am doing wrong. I am running a hosted service beside a webapi.