3

I have a class that calls an external service which requires a token, and this is injected using the Settings class.

Now, depending on the request, I need to use one of two tokens.

I would like to use middleware to look at the incoming request to then configure what is injected into my service, but not sure this is possible?

Can I configure the DI from middleware?

1 Answer 1

1

I'm not sure that you can configure Dependency Injection from within middleware because DI is done (if you're using the DI system supplied with .NET Core) during the Container setup faze (take a look at your startup.cs class).

I have a class that calls an external service which requires a token, and this is injected using the Settings class.

Now, depending on the request, I need to use one of two tokens

Could you not get both tokens and pass them into your middleware's constructor? I did something similar (passing a configuration object into the middleware constructor) when I was creating my OWASP secure headers middleware (it's part two in a now finished multipart series).

Could you take in the config and switch on something found within the request during your middleware's Invoke task?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.