1

My AutoFac implementation doesn't appear to be injecting any concrete instances of my interfaces into my API controllers.

Inspecting the Container object it does have a awareness of the UserRepo - it just does nothing with it.

Using latest version of AutoFac (Nuget) and AutoFac/WebAPI and Autofc/MVC5.

 public class ContainerConfig
    {
        public static void RegisterContainer()
        {
            var builder = new ContainerBuilder();

            builder.RegisterApiControllers();

            //var asm = typeof(IUserRepository).Assembly;
            builder.RegisterAssemblyTypes(typeof(UserRepository).Assembly)
                .Where(x => x.Name.EndsWith("Repository")).AsImplementedInterfaces();

            var container = builder.Build();
            GlobalConfiguration.Configuration.DependencyResolver= new AutofacWebApiDependencyResolver(container);
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }

public class UsersController : ApiController
{
    IUserRepository _db;

    public UsersController(IUserRepository db)
    {
        _db = db;
    }
}

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        Database.SetInitializer<FindABriefContext>(new DbInit());
        ContainerConfig.RegisterContainer();
    }
}

public class UserRepository : IUserRepository { DbContext db = null;

    public UserRepository(DbContext context)
    {
        db = context;
    }
}
Stuart.Sklinar
  • 3,683
  • 4
  • 35
  • 89
  • Have you tried builder.RegisterApiControllers(typeof(UsersController).Assembly); to indicate which assembly to scan for your controller. – obaylis Aug 06 '15 at 15:05
  • No - No I have not, but i *AM* registering API Controllers.. – Stuart.Sklinar Aug 06 '15 at 16:24
  • Found it - None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'UserRepository' can be invoked with the available services and parameters: Cannot resolve parameter 'DbContext' of constructor 'Void .ctor(DbContext)'. But, coming from a Unity background - this was something that was always done for us? Do i have to specify every class now? – Stuart.Sklinar Aug 06 '15 at 16:40
  • 1
    Yes, you must register every class you wish to participate in DI. You can potentially use the `AnyConcreteTypeNotAlreadyRegisteredSource` like `cb.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());`. See http://stackoverflow.com/questions/3413660/have-to-register-every-class-before-the-autofac-container-can-resolve – Travis Illig Aug 07 '15 at 14:30
  • Perfect - just what I was looking for. – Stuart.Sklinar Aug 07 '15 at 14:59

1 Answers1

1

I think the way you implement dependency injection for Web API is different to MVC. Instead of using AutofacDependencyResolver you should use AutofacWebApiDependencyResolver.

 var config = GlobalConfiguration.Configuration;
 config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
user2818985
  • 380
  • 3
  • 10