ASP.NET Core如何在ActionFilterAttribute里做依赖注入

文章来源:http://www.dalbll.com/group/topic/asp.net/6333

在ASP.NET Core里,我们可以使用构造函数注入很方便地对Controller,ViewComponent等部件做依赖注入。但是如何给过滤器ActionFilterAttribute也用上构造函数注入呢?

问题
我的博客系统里有个用来删除订阅文件缓存的ActionFilter,想要在发生异常的时候记录日志。我的博客用的日志组件是NLog,因此不使用依赖注入的话,就直接使用LogManager.GetCurrentClassLogger()获得一个Logger的实例。整个过滤器的代码如下:

 1 public class DeleteSubscriptionCache : ActionFilterAttribute
 2 {
 3     private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
 4 
 5     public override void OnActionExecuted(ActionExecutedContext context)
 6     {
 7         base.OnActionExecuted(context);
 8         DeleteSubscriptionFiles();
 9     }
10 
11     private void DeleteSubscriptionFiles()
12     {
13         try
14         {
15             // ...
16         }
17         catch (Exception e)
18         {
19             Logger.Error(e, "Error Delete Subscription Files");
20         }
21     }
22 }

然后在Action上去使用,和经典的ASP.NET MVC一样

1 [Authorize]
2 [HttpPost, ValidateAntiForgeryToken, DeleteSubscriptionCache]
3 [Route("manage/edit")]
4 public IActionResult Edit(PostEditModel model)

这当然可以没有问题的运行,但写代码最重要的就是逼格,这个代码耦合了NLog,而我的博客系统里其他地方早就在用ASP.NET Core的ILogger接口了。如果哪天日志组件不再用NLog了,那么这个地方的代码就得改,而使用ILogger接口的代码就不需要动。虽然这种情况是绝对不会发生的,但是写代码一定要有追求,尽可能过度设计,才能不被人鄙视,然后才能面试造航母,工作拧螺丝。因此我决定把日志组件用依赖注入的方式安排一下。

改造过滤器
方法和在Controller中使用依赖注入完全一样,我们使用构造函数注入ILogger<DeleteSubscriptionCache>类型。于是代码变成了这样:

 1 public class DeleteSubscriptionCache : ActionFilterAttribute
 2 {
 3     protected readonly ILogger<DeleteSubscriptionCache> Logger;
 4 
 5     public DeleteSubscriptionCache(ILogger<DeleteSubscriptionCache> logger)
 6     {
 7         Logger = logger;
 8     }
 9 
10     public override void OnActionExecuted(ActionExecutedContext context)
11     {
12         base.OnActionExecuted(context);
13         DeleteSubscriptionFiles();
14     }
15 
16     private void DeleteSubscriptionFiles()
17     {
18         try
19         {
20             // ...
21         }
22         catch (Exception e)
23         {
24             Logger.LogError(e, "Error Delete Subscription Files");
25         }
26     }
27 }

但是问题来了,这样的话我们是没法在Action上无脑使用了,因为构造函数要求传参。如果要自己new一个的话,装逼就失败了。我们来看看正确的解决方法~

ServiceFilter
其实ASP.NET Core里,我们可以使用ServiceFilter来完成这个需求。它也是一种Attribute,可以作用在Action上。位于Microsoft.AspNetCore.Mvc.Core程序集里,定义如下:

 1 // A filter that finds another filter in an System.IServiceProvider.
 2 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
 3 public class ServiceFilterAttribute : Attribute, IFilterFactory, IFilterMetadata, IOrderedFilter
 4 {
 5         public ServiceFilterAttribute(Type type);
 6         public int Order { get; set; }
 7         public Type ServiceType { get; }
 8         public bool IsReusable { get; set; }
 9         public IFilterMetadata CreateInstance(IServiceProvider serviceProvider);
10 }

ServiceFilter允许我们解析一个已经添加到IoC容器里的服务,因此我们需要把DeleteSubscriptionCache注册一下:
services.AddScoped<DeleteSubscriptionCache>();
然后就能直接使用了:

1 [Authorize]
2 [HttpPost, ValidateAntiForgeryToken]
3 [ServiceFilter(typeof(DeleteSubscriptionCache))]
4 [Route("manage/edit")]
5 public IActionResult Edit(PostEditModel model)

运行时发现ILogger已经能被实例化了,完美!

猜你喜欢

转载自www.cnblogs.com/TbKing-blogs/p/11233057.html