【问题标题】:Autofac - Property Injection in Action Filter in ASP.NET CoreAutofac - ASP.NET Core 中的动作过滤器中的属性注入
【发布时间】:2019-03-18 18:26:15
【问题描述】:

在使用 ASP.NET Web API 2 编写应用程序时,我设法使用 Autofac 在过滤器级别实现了Property Injection

(以下示例属于非核心 ASP.NET Web API)

builder.Register(x => new MyCustomGlobalActionFilter())
    .AsWebApiActionFilterOverrideFor<MyCustomController>()
    .InstancePerRequest()
    .PropertiesAutowired();

有几点要提:

  • 我们应该为任何这样的控制器注册它:.AsWebApiActionFilterOverrideFor&lt;MyCustomController&gt;()
  • 以下位用于启用属性注入.PropertiesAutowired()

动作过滤器本身看起来有点不寻常,只要它与 Autofac 密切相关——我们实现了IAutofacActionFilter 接口。

然后我可以通过属性注入在过滤器级别解析服务,这是代码示例:

public class MyCustomGlobalActionFilter : IAutofacActionFilter
{
    public Session Session { get; set; }
    public DbContextWithUserAuditing DbContext { get; set; }
    public ITenantService TenantService { get; set; }

    public Task OnActionExecutingAsync(
        HttpActionContext actionContext, 
        CancellationToken cancellationToken
        )
    {
        string userId = null;
        int? tenantId = null;

        var claimsIdentity = actionContext.RequestContext.Principal as ClaimsPrincipal;

        // do some stuff

        return Task.FromResult(0);
    }

    public Task OnActionExecutedAsync(
        HttpActionExecutedContext actionExecutedContext,
        CancellationToken cancellationToken
        )
    {
        return Task.FromResult(0);
    }
}

因此,为了将服务解析为属性,我们只需将它们声明如下:

public Session Session { get; set; }
public DbContextWithUserAuditing DbContext { get; set; }
public ITenantService TenantService { get; set; }

我的问题:有没有办法通过在ASP.NET Core 中使用 Autofac 的过滤器中的属性注入来解析服务?

【问题讨论】:

  • 您是否已搜索过此问题的答案或尝试过任何无效的方法?在asp.net core action filter dependency injection 上快速搜索Google 会产生this blog article,这建议您使用[ServiceFilter(MyFilterType)] 将您的过滤器推过DI。在 Autofac 中使用 PropertiesAutowired 注册您的类型会得到您想要的。
  • 嗨@TravisIllig,感谢您的回复。用PropertiesAutowired 尝试过,它不能解析属性。请参阅下面关于依赖注入的答案。

标签: c# asp.net-core inversion-of-control autofac


【解决方案1】:

好吧,这实际上并没有回答我的问题,但仍然有些人可能会觉得它很有用。

而不是遵循 Service Locator 方法,通过 ASP.NET Core 中的过滤器,您几乎可以使用 依赖注入

享受吧!

public class MyCustomFilter : IAsyncActionFilter
{
    private Session _session;
    private DBContextWithUserAuditing _dbContext;
    private ITenantService _tenantService;

    public MyCustomFilter(
        Session session,
        DBContextWithUserAuditing dbContext,
        ITenantService tenantService
        )
    {
        _session = session;
        _dbContext = dbContext;
        _tenantService = tenantService;
    }

    public async Task OnActionExecutionAsync(
        ActionExecutingContext context,
        ActionExecutionDelegate next
        )
    {
        string userId = null;
        int? tenantId = null;

        // do stuff
        // ...

        var resultContext = await next();
    }
}

解决了主要难题 - 我们至少不使用 Service Locator

恕我直言,Property Injection 不是很重要,所以我理解为什么 Autofac 团队没有特别急于实施它。

【讨论】:

  • 如果它不起作用,那不是因为Autofac没有实现它;这将是由于 ASP.NET Core 解决它的方式。 Autofac 没有任何额外的实现。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-27
相关资源
最近更新 更多