Autofac属性注射失败的属性 [英] Autofac attribute injection failing on attributes

查看:374
本文介绍了Autofac属性注射失败的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在这几个问题,但他们往往指向我下面确切的文件...但它仍然没有工作。

I've found a few questions on this, but they tend to point to the exact documentation I'm following... but it's still not working.

我要建一个相当简单的ASP.NET MVC 4个网站,而该计划是使用 ActionFilterAttribute 基于日志记录。我有一个 DataAccessProvider 类打开与数据库事务,并提供单位的工作实例,我试图将其注入过滤属性。

I'm building a fairly simple ASP.NET MVC 4 site, and the plan is to use ActionFilterAttribute-based logging. I have a DataAccessProvider class which opens transactions with the database and provides unit-of-work instances, and I'm trying to inject it into the filter attribute.

的<一个href=\"http://$c$c.google.com/p/autofac/wiki/MvcIntegration#Inject_Properties_Into_FilterAttributes\">documentation说,这是不够的,只是叫 RegisterFilterProvider(),并确保相关类型注册。它特别指出,没有必要注册属性,但我既没有试过。我的code目前看起来是这样的:

The documentation says that it's enough to just call RegisterFilterProvider(), and ensure that the relevant types are registered. It specifically says that there is no need to register the attribute, but I've tried both with and without. My code currently looks something like this:

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

在文档的例子然后就放置在过滤器的属性,所以我做了相同的:

The example in the docs then just places a property on the filter, so I've done the same:

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

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

该文档说一切都需要 - 即使不是一个构造函数来注入;它是由物业注射完成。当我运行这个code,然而,数据访问属性始终是; Autofac似乎忽略它。我知道报名工作正常,因为它是正确的注射 EntityAccessProvider 进入我的控制器,但它不工作的属性。我缺少什么?

The docs say that's all is required - not even a constructor to inject into; it's done by property injection. When I run this code, however, The DataAccess property is always null; Autofac seems to ignore it. I know the registration works properly because it's correctly injecting EntityAccessProvider into my controllers, but it's not working for attributes. What am I missing?

推荐答案

您类型的财产 IDataAccessProvider 必须是公开作为注射工作。您还可以标记 DebugLogAttribute IDataAccessProvider 和它的实现内部,如果你preFER。

Your property of type IDataAccessProvider has to be public for injection to work. You can still mark DebugLogAttribute, IDataAccessProvider and it's implementation as internal if you prefer.

[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 {}

这篇关于Autofac属性注射失败的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆