2013-07-28 25 views
11

これについていくつかの質問がありましたが、私が次のドキュメントを指している傾向にありますが、まだ機能していません。属性でAutofac属性の注入に失敗しました

かなり単純なASP.NET MVC 4サイトを構築しており、ActionFilterAttributeベースのログを使用する予定です。私はDataAccessProviderデータベースとトランザクションを開き、作業単位のインスタンスを提供するクラスを持って、私はそれをフィルター属性に注入しようとしています。

documentationは、RegisterFilterProvider()を呼び出して、関連するタイプが登録されていることを確認するだけで十分です。具体的には、属性を登録する必要はないと言われていますが、私は、属性の有無にかかわらず試しました。ドキュメントはそれがすべてだと言う

public class DebugLogAttribute : ActionFilterAttribute 
{ 
    private IDataAccessProvider DataAccess { get; set; } 

    public override void OnActionExecuting(ActionExecutingContext filterContext) { ... } 
    public override void OnActionExecuted(ActionExecutedContext filterContext) { ... } 
} 

:ドキュメント内

var builder = new ContainerBuilder(); 
builder.RegisterControllers(Assembly.GetExecutingAssembly()); 

builder.Register(x => new EntityAccessProvider()) 
    .As<IDataAccessProvider>() 
    .InstancePerHttpRequest(); 

builder.RegisterType<DebugLogAttribute>().PropertiesAutowired(); 
//^I've tried it with and without this line 

builder.RegisterFilterProvider(); 
var container = builder.Build(); 

DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 

例そしてちょうどフィルターのプロパティを置くので、私は同じことをやった:私のコードは、現在、このようなものが見えます必須 - 注入するコンストラクタさえありません。それはプロパティの注入によって行われます。ただし、このコードを実行すると、DataAccessプロパティは常にnullになります。オートファックはそれを無視しているようだ。コントローラーに正しくEntityAccessProviderが注入されているため、登録が正しく機能していますが、属性に対しては機能していません。私は何が欠けていますか?

答えて

13

タイプIDataAccessProviderのあなたの財産は、注射のために公開である必要があります。 DebugLogAttributeIDataAccessProviderのマークを付けることができ、好きな場合は内部実装として実装できます。

[DebugLogAttribute] 
public class HOmeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 
} 

internal class DebugLogAttribute : ActionFilterAttribute 
{ 
    public IDataAccessProvider DataAccess { get; set; } 

    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     Debugger.Break(); 
    } 

    public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     Debugger.Break(); 
    } 
} 

internal interface IDataAccessProvider {} 

internal class DataAccessProvider:IDataAccessProvider {} 
+0

おっと...完全にそれを逃しました。ありがとう! – anaximander

+0

ありがとうございました:) – Redplane

関連する問題