无法使用AutoFac注入ActionFilter实例

时间:2014-02-19 12:59:14

标签: autofac asp.net-mvc-5.1

有人可以告诉我哪里出错了。我有一个具有以下自定义过滤器属性的操作...

[UserValidation("Standard")]
public ActionResult Index()

自定义属性定义为...

public class UserValidationAttribute : FilterAttribute, IAutofacActionFilter
{
    public ILogger Logger { get; set; }

    private string _status;

    public UserValidationAttribute (string status)
    {
        // At this point in the code, 'Logger' is null
        _status = status;
    }

    public void OnActionExecuting(HttpActionContext actionContext)
    {
    }

    public void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
    }
}

我正在构建AutoFac容器......

var builder = new ContainerBuilder();

builder.RegisterControllers(Assembly.GetExecutingAssembly());

builder.Register(x => new Logger()
                .As<ILogger>()
                .InstancePerHttpRequest();

builder.RegisterFilterProvider();

var container = builder.Build();

System.Web.Mvc.DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

由于某些原因,当自定义操作过滤器的构造函数被“命中”时,公共“Logger”属性为空?

任何帮助都将不胜感激。

由于

0 个答案:

没有答案