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.

Autowiring properties in base classes not working in .net Core 2

See original GitHub issue

I have a .Net Core 2 app and an API controller class which inherits from an abstract base class:

 [Route("api/[controller]")]
 public class MyController : BaseController
   

On the base controller I have:

public ICommandProcessor CommandProcessor { get; set; }

In my StartUp.cs I have:

builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
                .Where(t => t.IsSubclassOf(typeof(BaseController)))
                .PropertiesAutowired();

However, the CommandProcessor property is always null. I have tried moving it to the derived class and tried registering the type directly as follows:

builder.RegisterType<MyController>().PropertiesAutowired();

and the also the base class:

builder.RegisterType<BaseController>().PropertiesAutowired();

I have tried what is suggested here and here amongst other posts but nothing seems to work, which makes me think that it is an issue in .Net Core 2.

When I inject the ICommandProcessor into the controller constructer it is wired up fine.

Any ideas?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
tilligcommented, Sep 21, 2017
0reactions
daodolcommented, Apr 19, 2019

Everything is working as expected.

services.AddMvc().AddControllersAsServices();

builder.Register(cc =>
            {
                return ConcreteType();
            }).As<IInterface>().InstancePerLifetimeScope();

builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
                .Where(t => t.IsSubclassOf(typeof(BaseController)))
                .PropertiesAutowired();
public abstract class BaseController : Controller
{
    // Make sure you have a public setter
    public IInterface Smth { get; set; }
 }
public ConcreteType : IInterface 
{
//..................
}

in aspnetcore 2.2.it is not work

Read more comments on GitHub >

github_iconTop Results From Across the Web

Autowiring properties in base classes not working in ASP. ...
I have a .Net Core 2 app and an API controller class which inherits from an abstract base class: [Route("api/[controller]")] public class ......
Read more >
Dependency injection into controllers in ASP.NET Core
Discover how ASP.NET Core MVC controllers request their dependencies explicitly via their constructors with dependency injection in ASP.
Read more >
5. The IoC container
It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to...
Read more >
Top 10 Most Common Spring Framework Mistakes
Common Mistake #5: Improperly Dealing with Multithreading. Regardless of whether it is encountered in desktop or web apps, Spring or no Spring, multithreading ......
Read more >
Spring - IoC Container (With Examples)
It does not allow defining autowire with simple properties such as primitives, Strings, and Classes. This limitation is by design. When using ...
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