Dependency injection
I have 2 classes, 1 Wolt adapter and second one is Glovo adapter. Both of them need http clients to be injected but the problem is that when i run both of them Wolt's http client is not being injected but when i run onlt Wolt adapter it works. It does not even log anything. could somebody explain why?
35 Replies
https://codeshare.io/anqNPj
here is the code
when i run both wolt and glovo wolts http client does not even write Log inforamtion or console writeline
2025-09-07 16:07:23 [12:07:23 INF] Starting up the service
2025-09-07 16:07:23 [12:07:23 INF] Using SQLite database for Glovo state at: Data Source=/app/data/glovo_state.db
2025-09-07 16:07:23 [12:07:23 INF] Integration 'Glovo' is ENABLED. Setting up its background service.
2025-09-07 16:07:23 [12:07:23 INF] Integration 'Wolt' is ENABLED. Setting up its background service.
2025-09-07 16:07:23 [12:07:23 INF] [] Successfully configured HttpClient for GLovo Adapter.
2025-09-07 16:07:23 Successfully configured HttpClient for GLovo Adapter.
how do you know it's not being injected?
what code are you running that you expect to get a wolt httpclient?
it is here
GlovoAdapter and WoltAdaper both have HttpClient in their constructor
and i inject them from here
@Jimmacle
those adapters are registered from here
like it loops thru adapters (glovo, wolt) and checks which one is enabled and registeres them
is
WoltAdapter
ever actually instantiated then? the logging in your httpclient configuration method will only run when the httpclient is actually created
like, does the service work or do you get an error?what do you mean by actually created?
your httpclient configuration code only runs once you actually get a client from the factory
it doesn't run when you configure the services
in the loop i register 2 hosted services one for glovo adapter and second one is for wolt. When i ENABLE both of them Wolt does not start up, glovo works fine. But when i enable only wolt, it works
how are you injecting the httpclient into each service?
i've already seen that code, i mean woltadapter itself
this is just configuring a httpclientfactory
services.AddTransient<GlovoAdapter>();
services.AddTransient<WoltAdapter>();
I do this right after i wrote AddHttpClient
let's try again
you want me to run?
what code in WoltAdapter, like in the constructor, receives a http client from DI?
public class WoltAdapter(
ILogger<WoltAdapter> logger,
IProductRepository productRepository,
HttpClient httpClient,
IntegrationsConfig integrations) : IIntegrationAdapter
{
here i inject HttpClient httpClient
if you want i can call you and share screen
i just want to see evidence that the httpclient is the problem and not something else
are you sure you're getting 2 hosted services from this code when both adapters are enabled?
I also had that quesiton but when i look that up, it said that if the service is not properly configured it might not start up and it will stay silent (not throw erros)
but you can add logging for when the service successfully starts up
i do log every setp
so you know for a fact that you end up with 2 hosted services running when you're using both adapters?
that s the thing, wolt adapter is not being registered
and i htought its cuz http client is not registered correctly
if that was the case, you would get an exception
there is a reason i'm asking you to check that you actually get 2 hosted services
how can i check? for loop does not throw an error
log from inside the worker when it starts up
you said you log every step
yeah i log it and wolt does not appear
so what does that tell you
2025-09-07 16:58:03 [12:58:03 INF] [] Scheduled worker for 'Glovo' is starting with a 1 minute interval.
there has to be same but for Wolt
which means...
you are only ever registering one hosted service with this code
its not starting up or it is being overriden by glovo
there is nothing wrong with the woltadapter
Stack Overflow
Multiple Instances of HostedService
I want to run multiple instances of the same hosted service. I tried registering them twice:
services.AddHostedService<MyService>();
services.AddHostedService<MyService>();
But Execut...
"I want to run multiple instances of the same hosted service"
i have different instances. how does that relate to my problem?
you don't
you're trying to register 2 hosted services both of type
ScheduledIntegrationWorker
and the linked article (and the articles it links to) explains the problem with that
it also matches the behavior you're seeing, where only one hosted service ever works at a timeUnknown User•4w ago
Message Not Public
Sign In & Join Server To View
@Jimmacle Thanks man, fixed that bug