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
    0

    LightInject MVC IOC Causing BackOffice Tree Error

    Hi,

    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 1.0.0.4 LightInject.Web 1.0.0.4 LightInject 3.0.1.7

    My lightinject config is:

    public class LightInjectConfiguration
        {
            public static void Execute()
            {
                var container = new ServiceContainer();
                container.RegisterControllers();
                container.RegisterAssembly("UmbracoTest*.dll");
    
               // If I comment out this line, I don't get the exception but I don't know why.
                container.EnableMvc();
            }
        }
    

    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.

    EXCEPTION DETAILS:

    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

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Feb 23, 2016 @ 13:17
    Dave Woestenborghs
    0

    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
        builder.RegisterControllers(typeof(CurrentAssembly).Assembly);
        builder.RegisterApiControllers(typeof(CurrentAssembly).Assembly);
        builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
    
        // register umbraco application context
        builder.RegisterType<ApplicationContext>().AsSelf();
    
            // 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

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

    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

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Feb 23, 2016 @ 14:33
    Dave Woestenborghs
    0

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

    :-)

    Dave

  • Marcin Zajkowski 112 posts 585 karma points MVP 6x c-trib
    May 24, 2016 @ 01:48
    Marcin Zajkowski
    1

    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: https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/61745-Failed-to-retrieve-data-for-application-tree-forms-AutoFac-IOC-Umbraco-722-and-Forms 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 (http://stackoverflow.com/users/800103/seesharper) 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)
    container.RegisterControllers(Assembly.GetExecutingAssembly());
    container.RegisterApiControllers(Assembly.GetExecutingAssembly());
    
    //// Custom interfaces and services
    container.Register<IFoo>(factory => new Foo(container.GetInstance<IBar>());
    ...
    
    container.EnableMvc();
    container.EnablePerWebRequestScope();
    container.EnableWebApi(GlobalConfiguration.Configuration);
    
    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 4908 posts 12265 karma points MVP 4x admin c-trib
    Dec 28, 2017 @ 14:38
    Jeroen Breuer
    0

    Be careful if you want to use LightInject with Umbraco Cloud. See this topic: https://our.umbraco.org/forum/umbraco-cloud/89853-using-lightinject-on-umbraco-cloud

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft