C# 如何在ASP.NET CORE中使用带有依赖项注入的操作筛选器?
我在我的C# 如何在ASP.NET CORE中使用带有依赖项注入的操作筛选器?,c#,asp.net,asp.net-mvc,asp.net-core,action-filter,C#,Asp.net,Asp.net Mvc,Asp.net Core,Action Filter,我在我的ASP.NET CORE应用程序的任何地方都使用基于构造函数的依赖项注入,我还需要在我的操作过滤器中解析依赖项: public class MyAttribute : ActionFilterAttribute { public int Limit { get; set; } // some custom parameters passed from Action private ICustomService CustomService { get; } // this
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()
{
...
[ServiceFilter(typeof(TrackingAttribute), Order = 2)]
如果我将ICustomService放在构造函数中,那么我就无法编译我的项目。那么,如何在操作筛选器中获取接口实例呢?您可以使用
服务定位器
:
public void OnActionExecuting(ActionExecutingContext actionContext)
{
var service = actionContext.HttpContext.RequestServices.GetService<IService>();
}
public void OnActionExecuting(ActionExecutingContext actionContext)
{
var service=actionContext.HttpContext.RequestServices.GetService();
}
请注意,泛型方法GetService
是一个扩展方法,位于名称空间Microsoft.Extensions.DependencyInjection
中
如果要使用构造函数注入,请使用
TypeFilter
。请参阅如果您想避免服务定位器模式,您可以使用带有类型过滤器的构造函数注入DI
在您的控制器中使用
[TypeFilter(typeof(MyActionFilterAttribute), Arguments = new object[] {10})]
public IActionResult() NiceAction
{
...
}
您的ActionFilterAttribute
不再需要访问服务提供商实例
public class MyActionFilterAttribute : ActionFilterAttribute
{
public int Limit { get; set; } // some custom parameters passed from Action
private ICustomService CustomService { get; } // this must be resolved
public MyActionFilterAttribute(ICustomService service, int limit)
{
CustomService = service;
Limit = limit;
}
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
await next();
}
}
对我来说,注释[TypeFilter(typeof(MyActionFilterAttribute),Arguments=new object[]{10})]
似乎有些笨拙。为了获得更可读的注释,如[MyActionFilter(Limit=10)]
您的过滤器必须继承自TypeFilterAttribute
。我的回答显示了这种方法的一个示例。您可以使用服务过滤器来实例化控制器中所需的ActionFilters
在控制器中:
[MyAttribute(Limit = 10)]
public IActionResult()
{
...
[ServiceFilter(typeof(TrackingAttribute), Order = 2)]
您需要在依赖项容器中注册TrackingAttribute,以便ServiceFilter能够解析它
在上阅读更多关于此的信息一个好的选择是这样做(在.NET Core 3.1中测试):
在筛选器类中放置以下内容:
公共静态类筛选器容器
{
在Startup.cs/ConfigureServices(IServiceCollection services)中,放置以下内容:
services.AddControllers().addmvcopions(options=>options.Filters.Add(newfiltercontainer.GenericFilter())
结果是,对.NET Core应用程序中任何类型操作的请求都将通过此管道进出,而不会在任何操作之上声明筛选器属性。
让我向您展示Visual Studio输出窗口中的一个示例:
[action]: JSON STARTING
[action]: JSON FINISHED
[action]: JSON GIVING RESULT
[action]: JSON RESULT GIVEN. Value: TestId: 103, FullName:...
您能否在属性CustomService中添加setter以使其也可写?并在构造函数中添加ICCustomService作为参数?的可能重复项的可能重复项如果需要异步,您还可以使用iasyncationFilter
而不是IActionFilter
。这是一个很好的建议,但是需要更多的工作来对其进行单元测试。