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.
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.
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));
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).
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!
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:
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
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.
Dave
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
LOL was hoping that you got it working with Light Inject. Umbraco core has this on the roadmap as IOC container.
:-)
Dave
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:
And now it's working! Going to test it deeply and compare performance with previously one achieved with Ninject. Happy coding!
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
is working on a reply...