Copied to clipboard

Flag this post as spam?

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

  • Dave S 13 posts 77 karma points
    Feb 23, 2016 @ 10:55
    Dave S

    LightInject MVC IOC Causing BackOffice Tree Error


    I've installed LightInject MVC for my IOC and i'm getting a strange error in the back office, It won't render the tree. The front end still serves content pages but with LightInject being called I can't use the tree in the backoffice. If I comment out the line container.EnableMvc(); for LightInject, then it works but then it doesn't inject my dependencies.

    Can anyone shed any light please?

    I'm running VS 2015 & SQL Server 2014 on Windows 8.1 Pro.

    UmbracoVersion: 7.3.7

    The nuget versions i've got installed are: LightInject.Mvc LightInject.Web LightInject

    My lightinject config is:

    public class LightInjectConfiguration
            public static void Execute()
                var container = new ServiceContainer();
               // If I comment out this line, I don't get the exception but I don't know why.

    My StackTrace is:

    Received an error from the server Failed to retrieve data for application tree content

    Object reference not set to an instance of an object.


    System.NullReferenceException: Object reference not set to an instance of an object. STACKTRACE:

    at Umbraco.Web.Trees.ApplicationTreeExtensions.TryLoadFromControllerTree(ApplicationTree appTree, String id, FormDataCollection formCollection, HttpControllerContext controllerContext) at Umbraco.Web.Trees.ApplicationTreeController.

    Many thanks,


  • Dave Woestenborghs 3385 posts 11463 karma points MVP 6x admin c-trib
    Feb 23, 2016 @ 13:17
    Dave Woestenborghs

    Hi Dave,

    Never used light inject. But for Autofac you need to do some additional registration.

    Here is our autofac code set up :

    Maybe this can help you solve the issue.

            var builder = new ContainerBuilder();
            // Register the UmbracoContext as a factory, the controllers require this in their constructor
        builder.Register(c => UmbracoContext.Current).AsSelf();
        // register controllers
        // register umbraco application context
            // register your interfaces here
        // create container 
        var container = builder.Build();
        // setup the mvc dependency resolver to user autofac
        var resolver = new AutofacWebApiDependencyResolver(container);
        GlobalConfiguration.Configuration.DependencyResolver = resolver;
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));


  • Dave S 13 posts 77 karma points
    Feb 23, 2016 @ 14:18
    Dave S

    Hi Dave,

    Thanks for getting in touch. I'll try out using AutoFac using your suggested config above and i'll let you know how I get on.

    Kind regards,


  • Dave Woestenborghs 3385 posts 11463 karma points MVP 6x admin c-trib
    Feb 23, 2016 @ 14:33
    Dave Woestenborghs

    LOL was hoping that you got it working with Light Inject. Umbraco core has this on the roadmap as IOC container.



  • Marcin Zajkowski 106 posts 578 karma points MVP 4x c-trib
    May 24, 2016 @ 01:48
    Marcin Zajkowski

    Hi Dave,

    I've played a little bit with LightInject too (mostly because I heard that it would be used in v8 like Dave mentioned above :)).

    I also struggled with the same error + a lot more issues with additional packages and libraries used in solution to which I wanted to add this IoC container.

    First of all, we need to also instal LightInject.WebApi package and register all api controller within our app. It solves some of the errors, but in my case, many of them were still occuring (for example with Umbraco Forms).

    I found some discussions, for example: and figured out that registration of specific DLLs is helping out to cover their unregistered services. Reading more and more about it, mostly inside a lot of posts by author of LightInject on StackOverflow ( I found that we need to perform some kind of scan of used libraries and dependencies and register them too. I found a method inside source code responsible for registration of WebApi controllers and adopted it a little bit to help me out performing the same for my "lost" dependencies.

    My complete code responsible for registration of container:

    var container = new ServiceContainer();
    //// We need to register used typed for all assemblies in domain / app
    foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
        var controllerTypes = assembly.GetTypes().Where(t => !t.IsAbstract && typeof(IHttpController).IsAssignableFrom(t));
        foreach (var controllerType in controllerTypes)
            container.Register(controllerType, new PerRequestLifeTime());
    //// Umbraco / CurrentAssebly registrations (+ API)
    //// Custom interfaces and services
    container.Register<IFoo>(factory => new Foo(container.GetInstance<IBar>());
    DependencyResolver.SetResolver(new LightInjectMvcDependencyResolver(container));

    And now it's working! Going to test it deeply and compare performance with previously one achieved with Ninject. Happy coding!

  • Jeroen Breuer 4866 posts 12165 karma points MVP 3x admin c-trib
    Dec 28, 2017 @ 14:38
    Jeroen Breuer

    Be careful if you want to use LightInject with Umbraco Cloud. See this topic:


Please Sign in or register to post replies

Write your reply to: