Copied to clipboard

Flag this post as spam?

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


  • Fabio Milheiro 74 posts 136 karma points
    Oct 05, 2013 @ 15:53
    Fabio Milheiro
    0

    Can't use IoC in Umbraco 6 UserControls?

    I have some user controls in an umbraco 6 instance.

    There's the need to put a repository in there and, after installing Autofac, I was expecting the property IRepository automatically injected.

    But that's always null.

    Here's my register events class that registers the container:

    public class RegisterEvents : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContainerBuilder builder = new ContainerBuilder();
    
            builder.RegisterType<Logger>().As<ILogger>();
            builder.RegisterType<Repository>().As<IRepository>();
    
            DependencyResolver.SetResolver(new AutofacDependencyResolver(builder.Build()));
            base.ApplicationStarted(umbracoApplication, applicationContext);
        }
    }

     

    If I do:

    DependencyResolver.Current.GetService<IEmailRepository>();

     

    The Email public constructor that accepts an ILogger is injected with an instance of the chosen implementation so I can see that the dependency resolver seems to be working fine except with usercontrols...

    I tried to implement DI the Umbraco4 way (webforms instead of MVC) but it didn't work.

    I am not looking forward to having my usercontrols converted to MVC views as they're somewhat complex and it would take too long to do that.

    I didn't want to do service location here but I feel I am stuck here with it. Any suggestions on how to have the properties injected?

Please Sign in or register to post replies

Write your reply to:

Draft