Dependency injection Autofac 2.6和Orchard 1.6-活性过滤器结构注入
首先是两件事Dependency injection Autofac 2.6和Orchard 1.6-活性过滤器结构注入,dependency-injection,orchardcms,autofac,orchardcms-1.6,actionfilterattribute,Dependency Injection,Orchardcms,Autofac,Orchardcms 1.6,Actionfilterattribute,首先是两件事 我试图实现一个动作过滤器,它记录动作开始和结束的时间 我很了解Autofac 3.0中的.AsActionFilter()方法,但是 该项目基于Orchard 1.6,已知该项目与autofac 2.6.xxx兼容。我们现在不想经历升级到Autofac 3.0的潜在漫长过程,因此.AsActionFilter()选项对我们不可用 另一个选项是将过滤器(它扩展了ActionFilterAttribute)设置为我们的基本控制器上的一个属性(顺便说一句,所有其他控制器都从中继承)。问题
public class GRMSActionLoggingFilter : ActionFilterAttribute {
private readonly IGRMSCoreServices _grmsCoreServices;
private readonly ILoggingService _loggingService;
public GRMSActionLoggingFilter(IGRMSCoreServices grmsCoreServices, ILoggingService loggingService) {
_grmsCoreServices = grmsCoreServices;
_loggingService = loggingService;
}
public GRMSActionLoggingFilter() { }
public override void OnActionExecuting(ActionExecutingContext actionContext) {...}
public override void OnActionExecuted(ActionExecutedContext actionContext) {...}
}
将属性指定给基本控制器:
// This currently compiles but will fail during run time as the IGRMSCoreSerivces and ILoggingService will both be null. Need to property inject these services somehow.
[GRMSActionLoggingFilter]
有人有办法做到这一点吗?您不能(轻松地)将运行时值注入属性。
这就是属性在C#中的工作方式-只能传递某些类型的常量值。你可以阅读更多关于它的内容
为了在Orchard中实现所需的功能,您需要将代码拆分为两个组件:
- 你在动作中加入的标记属性
- 操作筛选器类继承自
并实现FilterProvider
IActionFilter
filterContext.ActionDescriptor.GetCustomAttributes(…)
)。如果某个属性存在,则执行您的操作
在果园核心区有很多这种技术的例子。检查例如
主题属性
和主题过滤器
操作过滤器类。正在更新到1.7(当前1.x分支)是否合适?它是在Autofac 3.0上运行的。目前还不是一个真正的选项,因为我们即将为客户端发布测试版。我们正在考虑在截止日期过后执行升级,但我们(read,my technical lead=P)也希望在此之前登录action。我只是想让你知道,在(一个现在运行Autofac 3.0的果园)将近两年的时间里,我在谷歌上搜索了一个相关的问题,发现了这个,完全忘记了发布它,因此,它导致了我们正在研究的另一个与过滤器/注入相关的问题的解决方案,因此,谢谢。=)