外面还得声明相应的接收字段
标签:
原文:Asp.NETCore让FromServices回来目录
起因这两天,我忽然有点怀念 Asp.NET MVC 5 之前的时代,原因是我看到项目里面有这么一段代码(其实不止一段,几乎每个 Controller 都是)
[Route("home")] [ApiController] public class HomeController : ControllerBase { private readonly IConfiguration configuration; private readonly IHostingEnvironment environment; private readonly CarService carService; private readonly PostServices postServices; private readonly TokenService tokenService; private readonly TopicService topicService; private readonly UserService userService; public HomeController(IConfiguration configuration, IHostingEnvironment environment, CarService carService, PostServices postServices, TokenService tokenService, TopicService topicService, UserService userService) { this.configuration = configuration; this.environment = environment; this.carService = carService; this.postServices = postServices; this.tokenService = tokenService; this.topicService = topicService; this.userService = userService; } [HttpGet("index")] public ActionResult<string> Index() { return "Hello world!"; } }在结构函数里面声明了一堆依赖注入的实例,外面还得声明相应的接收字段,使用代码克隆扫描,零零散散的充满在各个 Controller 的结构函数中。在 Asp.NET MVC 5 之前,我们可以把上面的代码简化为下面的形式:
[Route("home")] [ApiController] public class HomeController : ControllerBase { [FromServices] public IConfiguration Configuration { get; set; } [FromServices] public IHostingEnvironment Environment { get; set; } [FromServices] public CarService CarService { get; set; } [FromServices] public PostServices PostServices { get; set; } [FromServices] public TokenService TokenService { get; set; } [FromServices] public TopicService TopicService { get; set; } [FromServices] public UserService UserService { get; set; } public HomeController() { } [HttpGet("index")] public ActionResult<string> Index() { return "Hello world!"; } }但是,在 .NETCore 中,,上面的这断代码是会报错的,原因就是特性:FromServicesAttribute 只能应用于 AttributeTargets.Parameter,导航到 FromServicesAttribute 检察源码
namespace Microsoft.AspNetCore.Mvc { /// <summary> /// Specifies that an action parameter should be bound using the request services. /// </summary> /// <example> /// In this example an implementation of IProductModelRequestService is registered as a service. /// Then in the GetProduct action, the parameter is bound to an instance of IProductModelRequestService /// which is resolved from the request services. /// /// <code> /// [HttpGet] /// public ProductModel GetProduct([FromServices] IProductModelRequestService productModelRequest) /// { /// return productModelRequest.Value; /// } /// </code> /// </example> [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)] public class FromServicesAttribute : Attribute, IBindingSourceMetadata { /// <inheritdoc /> public BindingSource BindingSource => BindingSource.Services; } }那么问题来了,AttributeUsage 是什么时候移除了 AttributeTargets.Property 呢?答案是:2015年11月17日,是一个叫做 Pranav K 的哥们革了 FromServiceAttribute 的命,下面是他的代码提交记录
Limit [FromServices] to apply only to parameters
https://github.com/aspnet/Mvc/commit/2a89caed05a1bc9f06d32e15d984cd21598ab6fb
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32685.html