1

Can't set up dependency injection via properties in a class...

I googled everything and looked through all the documentation, so I couldn't find anything how to configure on .net core mvc... Help me please

HomeController.cs

public class HomeController : Controller
{
    [Inject]
    protected IUserRepository UserRepository { get; set; }

    public HomeController()
    { }
}

DI

public class CompositionRoot : ICompositionRoot
{
   public void Compose(IServiceRegistry services)
   {
       services.RegisterScoped<IUserRepository>, UserRepository>();
   }
}

Constructor injection works well, but property injection fails.

Most likely you need to add some additional package and additional settings, but I do not know what

I use:

  • ASP.NET CORE MVC 3.1
  • LightInject 6.3.4 (latest version)
Nikita
  • 64
  • 10
  • Try to set property with local variable as: private IUserRepository userRepository; public IUserRepository objUserRepository{ set { this.userRepository = value; } get { return userRepository; } } – Sh.Imran Jul 21 '20 at 13:23
  • It didn't help, it doesn't depend on it. @imran – Nikita Jul 21 '20 at 15:05
  • 1
    If ctor injection is working, what you can do is just set your property instead of the setting a private field. `public HomeController(IUserRepository userRepository) { UserRepository = userRepository }` – Bon Macalindong Jul 22 '20 at 07:33
  • @BonMacalindong but I need to inject the dependency through a property, not a constructor – Nikita Jul 22 '20 at 08:53

0 Answers0