Copied to clipboard

Flag this post as spam?

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


  • Ivan 165 posts 543 karma points
    Feb 27, 2017 @ 10:14
    Ivan
    0

    Dependency Injection in ApplicationEventHandler. Bug?

    Umbraco v7.5.8

    I have bunch of problems with DI setup (shown below).

    1) Neither OnApplicationInitialized, nor OnApplicationStarted (and other) events firing if constructor takes parameter(s).

    2) Backoffice is broken. It's not possible to access a content node. Exception message is: An error occurred when trying to create a controller of type 'ContentController'. Make sure that the controller has a parameterless public constructor.

    // Application handlers
    public class UmbracoApplicationEventHandler : IApplicationEventHandler
    {   
        private IMenuManager _menuManager;
    
        public UmbracoApplicationEventHandler(IMenuManager menuManager)
        {
            _menuManager = menuManager;
        }
    
        public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Saving += UpdateMenu;
        }
    
        private void UpdateMenu(IContentService sender, SaveEventArgs<IContent> saveEventArgs)
        {
            _menuManager.UpdateMenu();
        }
    }
    
    // Unity config:
    public static class UnityConfig
    {
        public static void RegisterComponents()
        {
            var container = new UnityContainer();
    
            container.RegisterType<IMenuManager, MenuManager>();
    
            GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
        }
    
    }
    
    // Owin Startup:
    public class UmbracoStandardOwinStartup : UmbracoDefaultOwinStartup
    {
        public override void Configuration(IAppBuilder app)
        {
            //ensure the default options are configured
            base.Configuration(app);
    
            UnityConfig.RegisterComponents();
        }
    }
    
  • Lars-Erik Aabech 350 posts 1102 karma points MVP 8x c-trib
    Feb 27, 2017 @ 11:20
    Lars-Erik Aabech
    100

    AFAIK, Umbraco does not support IoC controllers yet. You can use DI for controllers by overriding the ASP.NET MVC DependencyResolver, but that only works for controllers, not other Umbraco components. (Like ApplicationEventHandler)

    This might get better for v8, and it will definitely be full blown when we get v9 on .net core.

  • Ivan 165 posts 543 karma points
    Feb 27, 2017 @ 11:57
    Ivan
    0

    Ok, I see. Thanks.

Please Sign in or register to post replies

Write your reply to:

Draft