ASP.NET Core has inbuilt support for the dependency injection (DI) for adding the dependency to the container and then consume it in our application. Constructor injection is a familiar, and the most used way to inject the dependencies. However, it may not be an ideal choice in certain situations like only a single action method within the controller requires the dependency. ASP.NET Core provides an attribute called FromServices
to inject the dependencies directly into the controller’s action method. In this post, we’ll find out how to use FromServices Attribute in ASP.NET Core.
FromServices Attribute in ASP.NET Core
First, let’s take a quick look at injecting dependencies via constructor injection. Imagine the following interface and its implementation.
public interface IAccountService { bool Validate(int accID); } public class AccountService : IAccountService { public bool Validate(int accID) { //ToDo - Validate account return true; } }
The service is configured in the ConfigureServices
method in the Startup
class. Like,
public void ConfigureServices(IServiceCollection services) { services.AddTransient<IAccountService, AccountService>(); }
If you wish to use this service in any of your controllers, you inject the dependency via a constructor. Here, by adding a service type to your controller as a constructor parameter, ASP.NET Core will try to resolve that type using its built-in service container.
public class ValuesController : Controller { private readonly IAccountService _accountService; public ValuesController(IAccountService accountService) { _accountService= accountService; } [HttpGet] public ActionResult<bool> Get(int accId) { return _accountService.Validate(accID); } }
Constructor injection is one of the standard way to inject the dependencies and it is suitable in most cases. However, there are situations where you might want to avoid this approach to clean code and reduce rework. Like,
- What if only a single action method within the controller requires the service?
- Adding a dependency to the existing controller constructor will result in updating the unit test cases.
In above cases, you may use FromServices
attribute to inject the dependency. Like,
public class ValuesController : Controller { [HttpGet] public ActionResult<bool> Get([FromServices] IAccountService accountService, int accId) { return accountService.Validate(accID); } }
So when we decorate the parameter with FromServices
attribute, this instructs the ASP.NET Core to get it from the services container and inject the matching implementation. This makes the code cleaner and will also reduce the work of modifying the existing unit test cases.
Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.
Thanks for the concise to-the-point explanation!
very good example
You are just great. Thanks for ur articles. They help me alot. Thanks again
This works great in a Controller, but what if I want to inject something directly into a method inside a class?
From what I can tell, classes only allow constructor injection.
Very concise and perfect. I love reading your posts… takes only few minute and with the help of examples, I get solution to problems before I run into them.
Thanks
Thanks Atif