Dependency Injection in ApplicationEventHandler. Bug?
Umbraco v7.5.8
I have bunch of problems with DI setup (shown below).
1) Neither OnApplicationInitialized, nor OnApplicationStarted (and other) events firing if constructor takes parameter(s).
2) Backoffice is broken. It's not possible to access a content node. Exception message is:
An error occurred when trying to create a controller of type 'ContentController'. Make sure that the controller has a parameterless public constructor.
// Application handlers
public class UmbracoApplicationEventHandler : IApplicationEventHandler
{
private IMenuManager _menuManager;
public UmbracoApplicationEventHandler(IMenuManager menuManager)
{
_menuManager = menuManager;
}
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Saving += UpdateMenu;
}
private void UpdateMenu(IContentService sender, SaveEventArgs<IContent> saveEventArgs)
{
_menuManager.UpdateMenu();
}
}
// Unity config:
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
container.RegisterType<IMenuManager, MenuManager>();
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
}
// Owin Startup:
public class UmbracoStandardOwinStartup : UmbracoDefaultOwinStartup
{
public override void Configuration(IAppBuilder app)
{
//ensure the default options are configured
base.Configuration(app);
UnityConfig.RegisterComponents();
}
}
AFAIK, Umbraco does not support IoC controllers yet. You can use DI for controllers by overriding the ASP.NET MVC DependencyResolver, but that only works for controllers, not other Umbraco components. (Like ApplicationEventHandler)
This might get better for v8, and it will definitely be full blown when we get v9 on .net core.
Dependency Injection in ApplicationEventHandler. Bug?
Umbraco v7.5.8
I have bunch of problems with DI setup (shown below).
1) Neither OnApplicationInitialized, nor OnApplicationStarted (and other) events firing if constructor takes parameter(s).
2) Backoffice is broken. It's not possible to access a content node. Exception message is: An error occurred when trying to create a controller of type 'ContentController'. Make sure that the controller has a parameterless public constructor.
AFAIK, Umbraco does not support IoC controllers yet. You can use DI for controllers by overriding the ASP.NET MVC DependencyResolver, but that only works for controllers, not other Umbraco components. (Like ApplicationEventHandler)
This might get better for v8, and it will definitely be full blown when we get v9 on .net core.
Ok, I see. Thanks.
is working on a reply...