InjectAttribute outside of components
See original GitHub issueI am missing a mechanism in Blazor’s dependency injection. If it already exists and I just missed it in Blazor’s code, please let me know. If not, here is my suggestion as a user story:
As a Blazor programmer, I would like to use the
InjectAttribute
in classes other thanBlazorComponent
, too.
Example in pseudo-code:
class DataAccess : IDataAccess
{
[Inject] <<-- This is what I am missing, member not filled by Blazor's DI
HttpClient Http { get; set; }
...
void DoSomething()
{
// Here I would like to use this.Http
}
}
@page "/"
@inject IDataAccess DataAccess
...
Use case:
I could implement a class DataAccess
that encapsulates all web API calls for a Blazor application. Blazor components receive an instance of this class using the InjectAttribute
. However, DataAccess
needs HttpClient
and should get it using dependency injection. As far as I have seen from Blazor’s code and based on some tests I did, the class DataAccess
would not receive any value from Blazor’s DI system.
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Angular2 - Use Dependency Injection outside Components
It's to make possible the injection within the class (and not into another class). The provider for the SomeDependency class need to be...
Read more >InjectAttribute Class (Microsoft.AspNetCore.Components)
Definition. Indicates that the associated property should have a value injected from the service provider during initialization.
Read more >Access to JSRuntime outside of a component : r/Blazor
I'm trying to encapsulate some JS without having to instantiate it as a service. [Inject] does not work. A constructor requiring it also...
Read more >ASP.NET Core Blazor dependency injection
This article explains how Blazor apps can inject services into components. Dependency injection (DI) is a technique for accessing services ...
Read more >Dependency Injection in React
It's possible to inject scalar data and functions, or services that will be responsible for logic that remains outside of the component.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Also to be clear, the primary reason why
InjectAttribute
exists is just to support the Razor@inject
syntax.We’re not trying to stop developers from using
[Inject]
directly, but for Razor-based components, it should never be necessary. It’s much nicer to use@inject TypeName PropertyName
instead. People should rarely see[Inject]
explicitly in their own code.I’m guessing you’re using it currently in a base class because of the lack of
partial
support, but that’s an issue we aim to address soon.Instead of using Inject attribute, you need to inject HttpClient in the DataAccess constructor and add IDataAccess to the services collection: