locked
Dpendency injection for background jobs RRS feed

  • Question

  • User2049551313 posted

    Hi!

    I'm doing a backgound job listening to a queue as per doc: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1&tabs=visual-studio#consuming-a-scoped-service-in-a-background-task

    Now, the problem is that some services need the user identity of the original request. Or at least some claims from there.

    Is there any way that I can register dependencies with the newly created scope?

    Like, I have two classes implementing an interface, say, IUserNameProvider, and one has the claims identity as constructor parameter, and one the username, and I want the one class to be used in a normal call, and the other in an independent scope.

    I was hoping to be able to register the other provider as singleton instance in that other scope.

    Is something like that possible? If not, is there a recommended way of doing it?

    Wednesday, December 4, 2019 4:43 PM

All replies

  • User-474980206 posted

    the user identity should be passed via the queue request, not injected via constructor.

    Wednesday, December 4, 2019 5:08 PM