Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Maciej Rakowski 11 posts 91 karma points
    Mar 25, 2017 @ 11:26
    Maciej Rakowski
    0

    Autofac not working

    Hi

    I'm trying to setup Autofac for Umbraco and I'm failing hard. It looks like Autofac is ignoring the configuration.

    My Repository is always ending up as null. What am I missing?

    Here's the Startup config

    namespace MyApp.Website.EventHandlers
    {
    public class ApplicationStartedEventHandler : IApplicationEventHandler
    {
        public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            RegisterAutoMapper.PopulateAutoMapper();
    
            var builder = new ContainerBuilder();
    
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
    
            builder.RegisterControllers(typeof(InstructorProfileStep2Controller).Assembly);
            builder.RegisterControllers(typeof(RegisterController).Assembly);
            builder.RegisterControllers(typeof(Umbraco.Forms.Web.Trees.DataSourceTreeController).Assembly);
    
            builder.RegisterType<InstructorRepository>().As<IInstructorRepository>().InstancePerLifetimeScope().PropertiesAutowired();
    
    
            var container = builder.Build();
            var resolver = new AutofacWebApiDependencyResolver(container);
            GlobalConfiguration.Configuration.DependencyResolver = resolver;
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    
        }
    }
    }
    

    Now the builder.RegisterControllers(typeof(InstructorProfileStep2Controller).Assembly); builder.RegisterControllers(typeof(RegisterController).Assembly); should register my controllers - one is RenderMvcController another one is SurfaceController.

    The implementation of Repository is as follows:

        namespace MyApp.DataAccess
    {
        public class InstructorRepository : IInstructorRepository
        {
    
            public InstructorProfileDto GetInstructorProfile(int id)
            {
                return null;
            }
        }
    }
    

    And inside Controller - Repository here is always null:

    public class InstructorProfileStep2Controller: RenderMvcController
    {
        public IInstructorRepository InstructorRepository { get; set; }
    
        public override ActionResult Index(RenderModel model)
        {
            InstructorProfileStep2Model viewModel = new InstructorProfileStep2Model(model.Content);
    
            InstructorRepository.GetInstructorProfile(1123);
    
            return base.Index(viewModel);
        }
    }
    

    And project structure

    enter image description here

    Thanks

  • Yakov Lebski 591 posts 2347 karma points
    Mar 25, 2017 @ 13:36
    Yakov Lebski
    0

    I think better way is to use constructor initialization like

     public void InstructorProfileStep2Controller(IInstructorRepository instructorRepository)
        {
        _instructorRepository=instructorRepository
        }
    

    For apply property auto-wired on controller you should enable it

    builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();

  • Maciej Rakowski 11 posts 91 karma points
    Mar 25, 2017 @ 13:47
    Maciej Rakowski
    0

    Hi Yakov

    That works. Although it's not the perfect solution for us. We would prefer to go with Repository being a autoinjected property. Having in mind the above solution works, do you have any ideas why the solution provided by me isnt't working?

  • Yakov Lebski 591 posts 2347 karma points
    Mar 25, 2017 @ 13:54
    Yakov Lebski
    100

    Did you try this one ?

    builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
    
  • Maciej Rakowski 11 posts 91 karma points
    Mar 25, 2017 @ 14:05
    Maciej Rakowski
    0

    Thanks!

    That worked. The only thing I had to either remove my

    builder.RegisterControllers(typeof(InstructorProfileStep2Controller).Assembly);
    builder.RegisterControllers(typeof(RegisterController).Assembly);
    

    or apply PropertiesAutowired() on them.

    Maciej

  • Yakov Lebski 591 posts 2347 karma points
    Mar 25, 2017 @ 14:10
    Yakov Lebski
    1

    I think you can register all controllers in one row

    builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
    

    or define per each controller

    builder.RegisterControllers(typeof(InstructorProfileStep2Controller).Assembly).PropertiesAutowired();
    builder.RegisterControllers(typeof(RegisterController).Assembly).PropertiesAutowired();
    
Please Sign in or register to post replies

Write your reply to:

Draft