【发布时间】:2017-01-08 10:35:47
【问题描述】:
我在我的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();
}
}
然后在控制器中:
[MyAttribute(Limit = 10)]
public IActionResult()
{
...
如果我将 ICustomService 放入构造函数,那么我将无法编译我的项目。那么,我应该如何在动作过滤器中获取接口实例?
【问题讨论】:
-
能否在属性 CustomService 中添加 setter 使其也可写?并在构造函数中添加 ICustomService 作为参数?
标签: c# asp.net asp.net-mvc asp.net-core action-filter