ApplicationEventHandler中的依赖注入。错误?

时间:2017-02-27 10:16:56

标签: umbraco

Umbraco v7.5.8

DI设置有很多问题(如下所示)。

1)如果构造函数接受参数,则不会触发OnApplicationInitialized,也不会触发OnApplicationStarted(和其他)事件。

2)后台打破了。无法访问内容节点。异常消息是: 尝试创建“ContentController”类型的控制器时发生错误。确保控制器具有无参数的公共构造函数。

    // 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();
    }
}

1 个答案:

答案 0 :(得分:2)

请阅读:https://our.umbraco.org/documentation/reference/using-ioc

您需要在 OnApplicationStarted 事件上注册和构建容器,而不是更早,如果您想使其与Umbraco一起使用。