简体   繁体   中英

Autofac - Property Injection in Action Filter in ASP.NET Core

While coding an app using ASP.NET Web API 2 I've managed to achieve Property Injection at filter level using Autofac .

(the example below belongs to non-core ASP.NET Web API)

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

A couple of things to mention:

  • we are supposed to register it for any controller like this: .AsWebApiActionFilterOverrideFor<MyCustomController>()
  • the following bit is used to enable Property Injection : .PropertiesAutowired()

The action filter itself looks a bit unusual as long as it's closely related to Autofac - we implement IAutofacActionFilter interface.

Then I can resolve services at filter level via property injection, here's code example:

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

So, in order to resolve services as properties we just declare them as follows:

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

My question: is there a way to resolve services via property injection in a filter using Autofac in ASP.NET Core ?

Well, this doesn't answer my question in fact, still some people might find it useful.

Rather than following Service Locator approach, with filter in ASP.NET Core you can pretty much use Dependency Injection .

Enjoy!

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

The major dilemma is resolved - we don't use Service Locator at least.

IMHO, Property Injection isn't very crucial, so I understand why Autofac team isn't in a particular rush implementing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM