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:
I don't get the sections on the left (Content, Media, Settings, etc), but I get the content tree.
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
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.
// 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:
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.
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: I don't get the sections on the left (Content, Media, Settings, etc), but I get the content tree. Here's the code I use for DI configuration:
Everything works fine when I disable DI, by commenting out this line
Any ideas? Sorry for the long post, and thanks for reading.
Finally, here's the error in the log file:
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
And that seems to work fine (on 7.6.x)
dave
Thank you for this Dave!!!!!!!!!!
Worked like a charm by registering Diplo & Forms api controllers.
Huge thanks, Dave, lightning-fast & spot-on response appreciated!
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
I found a way to do this generically:
Then you don't even need to worry about doing this:
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.
is working on a reply...