简体   繁体   English

解决全局动作过滤器中的依赖项?

[英]Resolving dependencies in global actionfilters?

I have inherited from and extended the HandleErrorAttribute of MVC3 with some custom logging. 我继承了MVC3的HandleErrorAttribute并对其进行了扩展,并添加了一些自定义日志记录。

One thing I am struggling to do though is to neatly resolve a dependency in it using Castle Windsor. 我正在努力做的一件事是使用Castle Windsor巧妙地解决其中的依赖性。

Generally I resolve these sorts of dependencies in an extension of the ControllerActionInvoker, but it seems that the HandleErrorAttribute does not pass through here. 通常,我在ControllerActionInvoker的扩展中解决了这些依赖关系,但是HandleErrorAttribute似乎没有通过这里。

Where is it invoked from that I can hook in and extend it? 从哪里可以调用它并将其扩展?

Thanks. 谢谢。

As an example of what I currently do: https://stackoverflow.com/a/6627002/148998 作为当前当前操作的示例: https : //stackoverflow.com/a/6627002/148998

HandleErrorAttribute是一个IExceptionFilter因此您可能还需要在InvokeExceptionFilters上覆盖InvokeExceptionFilters并将依赖项注入那里。

What I ended up doing was extending the ControllerActionInvoker and resolving any attribute dependencies there, specifically for the exception filters. 我最终要做的是扩展ControllerActionInvoker并在那里解决所有属性依赖关系,特别是对于异常过滤器。

The code: 编码:

  public class WindsorActionInvoker : ControllerActionInvoker
    {
        private readonly IKernel _kernel;

        public WindsorActionInvoker(IKernel kernel)
        {
            _kernel = kernel;
        }

        protected override ExceptionContext InvokeExceptionFilters(ControllerContext controllerContext, IList<IExceptionFilter> filters, System.Exception exception)
        {
            foreach (var actionFilter in filters.Where(actionFilter => !(actionFilter.GetType() == controllerContext.Controller.GetType())))
            {
                _kernel.InjectProperties(actionFilter);
            }

            return base.InvokeExceptionFilters(controllerContext, filters, exception);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM