【问题标题】:Autofac attribute injection failing on attributesAutofac 属性注入在属性上失败
【发布时间】:2013-07-28 08:15:23
【问题描述】:

我发现了一些关于此的问题,但它们往往指向我正在关注的确切文档......但它仍然无法正常工作。

我正在构建一个相当简单的 ASP.NET MVC 4 站点,并且计划使用基于 ActionFilterAttribute 的日志记录。我有一个DataAccessProvider 类,它打开与数据库的事务并提供工作单元实例,我正在尝试将其注入到过滤器属性中。

documentation 表示只需调用RegisterFilterProvider() 即可,并确保已注册相关类型。它特别说不需要注册属性,但我已经尝试过有和没有。我的代码目前看起来像这样:

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));

文档中的示例然后只是在过滤器上放置了一个属性,所以我做了同样的事情:

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

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

文档说这就是必需的——甚至不需要注入的构造函数;它是通过属性注入完成的。但是,当我运行此代码时,DataAccess 属性始终为null; Autofac 似乎忽略了它。我知道注册工作正常,因为它正确地将EntityAccessProvider 注入到我的控制器中,但它不适用于属性。我错过了什么?

【问题讨论】:

    标签: c# asp.net-mvc-4 autofac actionfilterattribute


    【解决方案1】:

    IDataAccessProvider 类型的属性必须是 public 才能使注入工作。您仍然可以将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 {}
    

    【讨论】:

    • 糟糕...完全错过了。谢谢!
    • 感谢您的解决方案 :)
    【解决方案2】:

    我在 asp dotnet core 中遇到了同样的问题,但当前的解决方案(将其公开)似乎不起作用。我觉得奇怪的是,下面的评论是关于 web-api 的,但我使用的是普通的 ASP.NET Core MVC (MVC6)。因此,如果有人遇到同样的问题,请尝试以下解决方案。

    https://docs.autofac.org/en/latest/integration/webapi.html#standard-web-api-filter-attributes-are-singletons

    与 MVC 中的过滤器提供程序不同,Web API 中的过滤器提供程序不允许您指定不应缓存过滤器实例。这意味着 Web API 中的所有过滤器属性实际上都是在应用程序的整个生命周期中存在的单例实例。

    public override async Task OnActionExecutionAsync(
                ActionExecutingContext context,
                ActionExecutionDelegate next)
            {
                MyService = context.HttpContext.
                                   RequestServices.GetService(typeof(IMyService)) as IMyService;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-01
      • 2017-01-26
      • 2012-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      相关资源
      最近更新 更多