[英]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 . 在使用ASP.NET Web API 2编写应用程序时,我设法使用Autofac在过滤器级别实现了属性注入 。
(the example below belongs to non-core ASP.NET Web API) (下面的示例属于非核心ASP.NET Web API)
builder.Register(x => new MyCustomGlobalActionFilter())
.AsWebApiActionFilterOverrideFor<MyCustomController>()
.InstancePerRequest()
.PropertiesAutowired();
A couple of things to mention: 有两件事要提到:
.AsWebApiActionFilterOverrideFor<MyCustomController>()
.AsWebApiActionFilterOverrideFor<MyCustomController>()
.PropertiesAutowired()
.PropertiesAutowired()
The action filter itself looks a bit unusual as long as it's closely related to Autofac - we implement IAutofacActionFilter
interface. 只要动作过滤器与Autofac密切相关,它看起来就有点与众不同-我们实现了
IAutofacActionFilter
接口。
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
? 我的问题: 有没有办法通过使用
ASP.NET Core
Autofac在过滤器中通过属性注入来解析服务?
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 . 不用遵循Service Locator方法,而是使用ASP.NET Core中的过滤器,您几乎可以使用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. 解决了主要的难题-至少我们不使用Service Locator 。
IMHO, Property Injection isn't very crucial, so I understand why Autofac team isn't in a particular rush implementing it. 恕我直言, 属性注入不是非常关键,所以我理解为什么Autofac团队并不急于实现它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.