2

開始有兩件事。Autofac 2.6和Orchard 1.6 - 行動過濾器施工注塑

  1. 我想實現一個動作過濾器,記錄時和動作開始,當它結束
  2. 我深知在Autofac 3.0 .AsActionFilter()方法,但...

這個項目使用的是Orchard 1.6,它被稱爲與autofac 2.6.xxx兼容。我們不希望在此時升級到Autofac 3.0,因此我們無法使用.AsActionFilter()選項。

另一種選擇是設置過濾器(它擴展了ActionFilterAttribute)作爲我們的基礎控制器(從中所有其他繼承btw)屬性。問題是,該過濾器本身有兩個依賴:

  1. 保存上下文
  2. 的ILoggingService的實現信息我們自己的服務

我不能找到一種方法來將這些注入到班級負責人的實際財產中。有沒有人知道在註冊期間通過Autofac的某些功能的[Attribute]行本身來實現這一目標的方法?

的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] 

任何人有任何想法,以實現這一目標?

+0

正在更新到1.7(當前1.x分支)爲你好嗎?它在Autofac 3.0上運行。 – 2013-05-07 20:23:53

+0

目前還不是一個真正的選擇,因爲我們正在接近客戶端的測試版。 我們正考慮在截止日期過後執行升級,但我們(請閱讀,我的技術主管= P)在此之前還想要採取行動登錄。 – 2013-05-08 08:16:10

回答

1

您不能(很容易)將運行時值注入屬性。 這是屬性在C#中的工作方式 - 您只能傳遞某些類型的常量值。你可以閱讀更多關於它here

爲了實現在果園,你需要你的代碼分爲兩個部分所需的功能:

  • 標記屬性類,你把你的行動
  • 行動過濾器類繼承FilterProvider和實施IActionFilter

它的工作方式是,你把一個屬性一些動作,然後使用動作過濾器來檢查該屬性的存在(使用filterContext.ActionDescriptor.GetCustomAttributes(...))。如果存在某個屬性,請執行你的操作。

在Orchard核心中有很多這種技術的例子。檢查例如。動作過濾器類爲ThemedAttributeThemeFilter

+0

只是想我會讓你知道,近兩年(Orchard現在運行Autofac 3.0)我搜索了一個相關的問題,發現這個,完全忘了發佈它,結果它導致了另一個過濾器的解決方案/我們在哪裏調查的注射相關問題,謝謝。 =) – 2015-01-13 15:23:08