文章来源:http://www.dalbll.com/group/topic/asp.net/6333面试
在ASP.NET Core里,咱们可使用构造函数注入很方便地对Controller,ViewComponent等部件作依赖注入。可是如何给过滤器ActionFilterAttribute也用上构造函数注入呢?缓存
问题
个人博客系统里有个用来删除订阅文件缓存的ActionFilter,想要在发生异常的时候记录日志。个人博客用的日志组件是NLog,所以不使用依赖注入的话,就直接使用LogManager.GetCurrentClassLogger()得到一个Logger的实例。整个过滤器的代码以下:asp.net
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同样ide
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>类型。因而代码变成了这样:spa
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一个的话,装逼就失败了。咱们来看看正确的解决方法~.net
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已经能被实例化了,完美!code