简体   繁体   English

没有[ServiceFilter]或[TypeFilter]的过滤器中的Asp.net核心依赖注入

[英]Asp.net Core dependency injection in filters without [ServiceFilter] or [TypeFilter]

I need to inject some services with dependency injection to action filters. 我需要注入一些带有依赖注入的服务到动作过滤器。 I'm familiar with the [ServiceFilter] and [TypeFilter] approach, but it's kind of ugly, messy, and unclear. 我熟悉[ServiceFilter][TypeFilter]方法,但它有点丑陋,凌乱,不清楚。

Is there a way I can set a filter in the normal way? 有没有办法按正常方式设置过滤器? without wrapping the filter I'm using with [ServiceFilter] or [TypeFilter] ? 没有包装我正在使用[ServiceFilter][TypeFilter]的过滤器?

For example what I want: 例如我想要的:

[SomeFilterWithDI]
[AnotherFilterWithDI("some value")]
public IActionResult Index()
{
    return View("Index");
}

Instead of: 代替:

[ServiceFilter(typeof(SomeFilterWithDI))]
[TypeFilter(typeof(AnotherFilterWithDI), Arguments = new string[] { "some value" })]
public IActionResult Index()
{
    return View("Index");
}

It looks way different, this approach doesn't seem right to me. 它看起来有所不同,这种方法对我来说似乎不对。

For [SomeFilterWithDI] , you could refer the comment from @Kirk larkin. 对于[SomeFilterWithDI] ,您可以参考@Kirk larkin的评论。

For [AnotherFilterWithDI("some value")] , you could try passing Arguments from TypeFilterAttribute . 对于[AnotherFilterWithDI("some value")] ,您可以尝试从TypeFilterAttribute传递Arguments

  • ParameterTypeFilter define the accept parameters. ParameterTypeFilter定义accept参数。

     public class ParameterTypeFilter: TypeFilterAttribute { public ParameterTypeFilter(string para1, string para2):base(typeof(ParameterActionFilter)) { Arguments = new object[] { para1, para2 }; } } 
  • ParameterActionFilter accept the passed parameters. ParameterActionFilter接受传递的参数。

      public class ParameterActionFilter : IActionFilter { private readonly ILogger _logger; private readonly string _para1; private readonly string _para2; public ParameterActionFilter(ILoggerFactory loggerFactory, string para1, string para2) { _logger = loggerFactory.CreateLogger<ParameterTypeFilter>(); _para1 = para1; _para2 = para2; } public void OnActionExecuting(ActionExecutingContext context) { _logger.LogInformation($"Parameter One is {_para1}"); // perform some business logic work } public void OnActionExecuted(ActionExecutedContext context) { // perform some business logic work _logger.LogInformation($"Parameter Two is {_para2}"); } } 

    As the description from Arguments , ILoggerFactory loggerFactory is resolved by dependency injection container . Arguments的描述, ILoggerFactory loggerFactorydependency injection container解析。 para1 and para2 is resolved by ParameterTypeFilter . para1para2ParameterTypeFilter解析。

      // // Summary: // Gets or sets the non-service arguments to pass to the Microsoft.AspNetCore.Mvc.TypeFilterAttribute.ImplementationType // constructor. // // Remarks: // Service arguments are found in the dependency injection container ie this filter // supports constructor injection in addition to passing the given Microsoft.AspNetCore.Mvc.TypeFilterAttribute.Arguments. public object[] Arguments { get; set; } 
  • Useage 使用率

     [ParameterTypeFilter("T1","T2")] public ActionResult Parameter() { return Ok("Test"); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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