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.
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.
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.
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.
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.
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.
The errors i am getting when i click in Content are the following:
System.AggregateException: One or more errors occurred.
at System.Threading.Tasks.Task
1.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, IDictionary
2 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.
Hi,
I think you need register context variables per request to avoid the wrong initialization
In my project, I do autofac init on OnApplicationInitialized event - can you move it to thie event
Hi Thomas,
I think you need to register that api controllers from Umbraco as well
Dave
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
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
is working on a reply...