Hi,
I’m creating a asp.net core BackgroundService and I need to inject a Service with a dynamic parameter based on a parameter passed by Queue. My job looks like this:


Any tips?
I’m not sure what you need. What is the dynamic parameter? And what is the parameter passed by Queue? What needs to be injected where? You show some code that doesn’t look like anything is dynamic i.e. has some condition that affects another part of the code.
Thanks for your reply. The dynamic parameter is the TenantId passed to TenantProvider constructor. It is a property from Queue Message.
So, I created a new TenantProvider instance but I dont know how to inject it to be used the PortfolioProcessService (and another class inside it).
You need to create a class similar to HttpContextAccessor.
You could create a TenantAccessor with a property of current Tenant, and have it registered as a Scoped dependency. Then when you create the scope in your background services, you can resolve the accessor and set the Tenant.
Alternative you can use a ThreadLocal variable similar to how HttpContext is stored.
C# devs
null reference exceptions

source