我在我的ASP.NET CORE应用程序中使用基于构造函数的依赖注入,我还需要在我的动作过滤器中解决依赖关系:
public class MyAttribute : ActionFilterAttribute
{
public int Limit { get; set; } // some custom parameters passed from Action
private ICustomService CustomService { get; } // this must be resolved
public MyAttribute()
{
}
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
// my code
...
await next();
}
}
Run Code Online (Sandbox Code Playgroud)
然后在控制器中:
[MyAttribute(Limit = 10)]
public IActionResult()
{
...
Run Code Online (Sandbox Code Playgroud)
如果我把ICustomService放到构造函数中,那么我就无法编译我的项目了.那么,我如何在动作过滤器中获取接口实例呢?