question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InjectAttribute outside of components

See original GitHub issue

I 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 than BlazorComponent, 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:closed
  • Created 5 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
SteveSandersonMScommented, Mar 29, 2018

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.

2reactions
aguacongascommented, Mar 29, 2018

Instead of using Inject attribute, you need to inject HttpClient in the DataAccess constructor and add IDataAccess to the services collection:

publis class DataAccess : IDataAccess
{
  private readonly HttpClient _http;
  
  public DataAccess(HttpClient http)
  {
      _http = http;
  }
  ...
  void DoSomething()
  {
    // Here I would like to use _http
  }
}
class Program
{
    static void Main(string[] args)
    {
         var serviceProvider = new BrowserServiceProvider(services =>
         {
             // Add any custom services here
            services.AddSingleton<IDataAccess, DataAccess>();
         });

        new BrowserRenderer(serviceProvider).AddComponent<App>("app");
    }
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found