Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
declanmc opened this issue Sep 21, 2017 · 7 comments
Closed

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

declanmc opened this issue Sep 21, 2017 · 7 comments

Comments

@declanmc
Copy link

declanmc commented Sep 21, 2017

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?

@tillig
Copy link
Member

tillig commented Sep 21, 2017

Did you add controllers as services?

@declanmc
Copy link
Author

I have just tried that and it has made no difference.

It is worth mentioning that I have tried this on other class that is not a controller, registered in the following way in the startup:

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

and the property on that is still null also

@tillig
Copy link
Member

tillig commented Sep 21, 2017

Have you asked this on StackOverflow? If not, that may be a better option as noted in the issue template. There are only two project maintainers for all of Autofac and the integration libraries so time is pretty limited to go back and forth on troubleshooting individual issues.

If you've asked there, please link back to the question here.

If not, give it a shot and link the question in here.

Either way, we'll need to see the app setup with ConfigureServices and all registrations. So far there's not enough info to figure out the issue.

@declanmc
Copy link
Author

I've raised the question on StackOverflow.

The ConfigureServices is below:

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc().AddControllersAsServices();

    // Identity server

    var builder = new ContainerBuilder();

    builder.RegisterModule(new CoreImplementationModule());
    builder.RegisterModule(new PortalCoreImplementationModule());

    builder.Register((context, _) => new DocumentClient(
                    new Uri(Configuration["DocumentDb:Uri"]),
            Configuration["DocumentDb:Key"],
            null,
            new ConsistencyLevel?()))
        .As<IDocumentClient>();

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

    builder.Populate(services);

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

    var container = builder.Build();

    return new AutofacServiceProvider(container);
}

@tillig
Copy link
Member

tillig commented Sep 21, 2017

Awesome. We can follow up on the question, then, and folks coming in with similar questions or answers can see the complete thing there.

If it turns out there's a bug, we can file something on the appropriate repo - this one for core Autofac, Autofac.Extensions.DependencyInjection for the adapter.

Thanks!

@tillig tillig closed this as completed Sep 21, 2017
@razvalex
Copy link

razvalex commented Mar 9, 2018

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 
{
//..................
}

@daodol
Copy link

daodol commented 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants