locked
[Inject] not working in class RRS feed

  • Question

  • User-505790528 posted

    In my startup.cs

     public void ConfigureServices(IServiceCollection services)
            {
                services.AddRazorPages();
                services.AddServerSideBlazor();
                services.AddScoped<TestInfo>();
            }

    and in another class I have this:

    using Microsoft.AspNetCore.Components;
    
    namespace Test
    {
        public class TestClass
        {
            [Inject]
            public TestInfo Info { get; set; }
    
            public string InfoState
            {
                get
                {
                    string s = "Info is null";
    
                    if (Info != null)
                        s = "Info is Set";
    
                    return s;
                }
            }
    
            public TestClass()
            {
                if(Info != null)
                    Info.ConnStr = "My Connection String";      // <-- Info is null at this point!
            }
        }
    }

    Why is the Info value  null in my TestClass?

    Saturday, July 25, 2020 10:40 AM

Answers

  • User-474980206 posted

    My TestClass is not a service.

    If I was doing this:

    services.AddScoped<TestInfo>();
    services.AddScoped<TestClass>();

    Then I would expect to have to do something like this:

    public TestClass(TestInfo info)
    {
    }

    But as TestClass is not a service I thought that I could just use [Inject]

    you can, but if your code creates the class instance, rather than part of the framework that supports DI, your code will need to implement the DI support.  The reason to register as a service, is because the object constructor in Services, supports this.

    The @Inject support is really for Razor components, which you don't define a constructor for.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 27, 2020 3:11 PM

All replies

  • User475983607 posted

    Your code never sets "Info" if "Info" is null.

    Saturday, July 25, 2020 12:44 PM
  • User-821857111 posted

    The [Inject] attribute) isn't available for use in services. Constructor injection must be used instead. 

    From https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-3.1#use-di-in-services

    Saturday, July 25, 2020 3:55 PM
  • User-505790528 posted

    Hi, I thought that the line:

    services.AddScoped<TestInfo>();

    Created a global instance of the type specified.

    If I use :

    @Inject TestInfo Info

    withing a razor page it works without assigning anything to Info.

    Monday, July 27, 2020 11:01 AM
  • User-505790528 posted

    My TestClass is not a service.

    If I was doing this:

    services.AddScoped<TestInfo>();
    services.AddScoped<TestClass>();

    Then I would expect to have to do something like this:

    public TestClass(TestInfo info)
    {
    }

    But as TestClass is not a service I thought that I could just use [Inject]

    Monday, July 27, 2020 11:04 AM
  • User753101303 posted

    Hi,

    And how do you create this class? DI does nothing magic. At some point you need to have some code - even if, not written or called by you - that will initialize those properties (makes sense also that this is not yet done when the constructor that creates the class run).

    Try perhaps https://blazor-university.com/dependency-injection/injecting-dependencies-into-blazor-components/ 

    Also according to https://github.com/dotnet/aspnetcore/issues/16294 it seems intended to support @inject in components rather than being used as a general DI mechanism.

    Monday, July 27, 2020 11:41 AM
  • User-474980206 posted

    My TestClass is not a service.

    If I was doing this:

    services.AddScoped<TestInfo>();
    services.AddScoped<TestClass>();

    Then I would expect to have to do something like this:

    public TestClass(TestInfo info)
    {
    }

    But as TestClass is not a service I thought that I could just use [Inject]

    you can, but if your code creates the class instance, rather than part of the framework that supports DI, your code will need to implement the DI support.  The reason to register as a service, is because the object constructor in Services, supports this.

    The @Inject support is really for Razor components, which you don't define a constructor for.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 27, 2020 3:11 PM
  • User-505790528 posted

    Thanks for the links above.

    So it turns out that you cannot use [Inject] in a "normal" class only in a Blazor component class (code behind) which is why it wasn't working.

    Tuesday, July 28, 2020 10:06 AM