Skip to content

Blazor dependency injection in helper class #26357

Discussion options

You must be logged in to vote

I don't want to manually pass IJSRuntime to the class because I don't want the components to be aware of internal implementation details such as reliance on IJSRuntime.

I'm afraid you have to, because otherwise how would it know which IJSRuntime to use? With dependency injection in general, there are multiple "scopes" representing different partitions of services. For example with Blazor Server, there are multiple concurrent users, and each one has a separate IJSRuntime instance. To obtain a service instance, you need to request it from a particular scope. Components can do this because they are attached to a particular renderer that holds a reference to the scope.

In general, the DI sy…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by SteveSandersonMS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants