Copied to clipboard

Flag this post as spam?

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


  • Ozkan Sayin 6 posts 136 karma points
    Sep 28, 2017 @ 19:08
    Ozkan Sayin
    0

    Umbraco Cloud (7.7.1) & Dependency Injection

    Hey there,

    I am trying to use Dependency Injection with an Umbraco Cloud instance on my local computer. I'm using Autofac (4.6.1), Autofac.Mvc5 (4.0.2) & Autofac.WebApi2 (4.1.0) Although it works fine for my controllers & classes, I am getting errors on the following requests when on the backoffice: enter image description here I don't get the sections on the left (Content, Media, Settings, etc), but I get the content tree. enter image description here Here's the code I use for DI configuration:

            var executingAssembly = Assembly.GetExecutingAssembly();
            builder.RegisterControllers(executingAssembly);
            builder.RegisterApiControllers(executingAssembly);
    
            builder.RegisterControllers(typeof(UmbracoApplication).Assembly);
            builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
    
            Startup.Init(builder);
            Business.ContainerInitializer.Initialize(builder);
            var container = builder.Build();
    
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    

    Everything works fine when I disable DI, by commenting out this line

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    

    Any ideas? Sorry for the long post, and thanks for reading.

    Finally, here's the error in the log file:

    2017-09-28 21:58:56,964 [P11936/D18/T37] ERROR Umbraco.Web.Editors.SectionController - Unhandled controller exception occurred System.AggregateException: One or more errors occurred. ---> System.NullReferenceException: Object reference not set to an instance of an object. at System.Object.GetType() at Umbraco.Web.Trees.ApplicationTreeExtensions.

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Sep 28, 2017 @ 19:33
    Dave Woestenborghs
    104

    Hi Ozkan,

    I think this is because Cloud has Diplo Trace log viewer and Umbraco forms installed.

    We have this in our Autofac config on Cloud

     // Register extensions
                builder.RegisterApiControllers(Assembly.Load("Diplo.TraceLogViewer"));
                builder.RegisterApiControllers(Assembly.Load("Umbraco.Forms.Web"));
    

    And that seems to work fine (on 7.6.x)

    dave

  • Dennis Adolfi 1082 posts 6445 karma points MVP 5x c-trib
    Dec 10, 2018 @ 11:09
    Dennis Adolfi
    0

    Thank you for this Dave!!!!!!!!!!

  • Ozkan Sayin 6 posts 136 karma points
    Sep 28, 2017 @ 19:41
    Ozkan Sayin
    0

    Worked like a charm by registering Diplo & Forms api controllers.

    Huge thanks, Dave, lightning-fast & spot-on response appreciated!

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Sep 28, 2017 @ 19:54
    Dave Woestenborghs
    0

    No problem,

    Basicly every package that creates a tree that inherits from TreeController needs to be registered. Haven't found a generic way yet.

    Dave

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Feb 08, 2019 @ 23:00
    Nicholas Westby
    0

    I found a way to do this generically:

    // Get all assemblies that have a tree controller.
    var treeType = typeof(Umbraco.Web.Trees.TreeController);
    var assemblies = AppDomain.CurrentDomain.GetAssemblies()
        .SelectMany(x =>
        {
            try
            {
                return x.GetTypes() ?? new Type[0];
            }
            catch
            {
                return new Type[0];
            }
        })
        .Where(x => treeType.IsAssignableFrom(x))
        .Select(x => x.Assembly)
        .DistinctBy(x => x.FullName)
        .ToList();
    
    // Register all controllers in assemblies containing tree controllers.
    foreach (var assembly in assemblies)
    {
        builder.RegisterControllers(assembly);
        builder.RegisterApiControllers(assembly);
    }
    

    Then you don't even need to worry about doing this:

    builder.RegisterControllers(typeof(UmbracoApplication).Assembly);
    builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
    

    From this documentation: https://our.umbraco.com/documentation/reference/using-ioc

    Because the above code finds that assembly too.

    Note that I don't really understand why this is necessary, but it seems to work, so thought I'd share the code that got it to work. Use this code with caution.

Please Sign in or register to post replies

Write your reply to:

Draft