Copied to clipboard

Flag this post as spam?

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


  • Thomas 160 posts 335 karma points
    Nov 06, 2018 @ 09:05
    Thomas
    1

    Umbraco Backend issue when setting up Umbraco API with Dependency Injection (AutoFac)

    Hi, Following the information in Umbraco Documentation i wrote the following code OnApplicationStarted event to use IoC and AutoFac.

            //register all MVC controllers and API controllers in the
            //current assembly in the container.
            var builder = new ContainerBuilder();
    
            //Register the UmbracoContext as a factory since the below controllers require this in their ctor
            builder.Register(c => UmbracoContext.Current).AsSelf();
            builder.RegisterInstance(ApplicationContext.Current).AsSelf();
            builder.RegisterInstance(new UmbracoHelper(UmbracoContext.Current)).AsSelf();
    
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces().InstancePerLifetimeScope();
    
            // register Umbraco MVC + web API controllers used by the admin site
            builder.RegisterControllers(typeof(UmbracoApplication).Assembly);
            builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
    
            // add custom class to the container as Transient instance
            RegisterTypes(builder);
    
            var container = builder.Build();
    
            //setup the webapi dependency resolver to use autofac
            var resolver = new AutofacWebApiDependencyResolver(container);
            GlobalConfiguration.Configuration.DependencyResolver = resolver;
    
            //setup the mvc dependency resolver to user autofac
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    

    Unfortunately, i am still getting YSOD errors in backend when i click on CONTENT menu. Also the umbraco menu items on the left side are not loaded and finally when i execute code to add content from code behind the content is not added.

    Error 1 when i login Error 2 when i click Content Menu (above HOME)

    The errors i am getting when i click in Content are the following:

    System.AggregateException: One or more errors occurred.

    at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at Umbraco.Web.Editors.SectionController.GetSections() at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary2 arguments, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ApiControllerActionInvoker.

    Inner Exception

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

    at Umbraco.Web.Trees.ApplicationTreeExtensions.

  • Yakov Lebski 591 posts 2347 karma points
    Nov 09, 2018 @ 07:53
    Yakov Lebski
    101

    Hi,

    I think you need register context variables per request to avoid the wrong initialization

    builder.Register(c => UmbracoContext.Current).InstancePerRequest();
    builder.Register(x => new UmbracoHelper(UmbracoContext.Current)).InstancePerRequest();
    

    In my project, I do autofac init on OnApplicationInitialized event - can you move it to thie event

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Nov 09, 2018 @ 08:52
    Dave Woestenborghs
    1

    Hi Thomas,

    I think you need to register that api controllers from Umbraco as well

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

    Dave

  • Thomas 160 posts 335 karma points
    Nov 27, 2018 @ 09:06
    Thomas
    0

    I will give a try and reply. I masked it as solution because i haven't done that. As for Daves suggestion, i have already include the API Controllers registration on my code and still getting the errors.

    Thanks a lot all of you

  • Marcio Barboza 1 post 71 karma points
    May 09, 2019 @ 05:55
    Marcio Barboza
    0

    Hi Thomas, did you come up with a solution for this issue? If so, please let us know your solution. I am getting the same issue, even though doing what Yakov and Dave instructed to do.

    Cheers, Marcio Barboza

Please Sign in or register to post replies

Write your reply to:

Draft