MVC Controllers Can Automatically Infer [FromServices] In .Net 7

Introduction

In .NET MVC 6.0, the [FromServices] attribute is used to indicate that a parameter in an action method should be resolved from the dependency injection container. This attribute can be used to inject dependencies such as services or repositories into your action method.

Here is an example of how you might use the [FromServices] attribute to inject a service into an action method:

using Microsoft.AspNetCore.Mvc;
namespace MyApp.Controllers {
    public class HomeController: Controller {
        private readonly MyService _service;
        public HomeController(MyService service) {
            _service = service;
        }
        //πŸ‘‡ Required in .NET 6
        public IActionResult Index([FromServices] MyService service) {
            // Use the service parameter to call a method on the service
            var result = service.DoSomething();
            return View(result);
        }
    }
}

Notice that in .NET 6, the [FromServices] annotation is necessary, otherwise, MVC tries to bind the links parameter to the request body.

In the above example, the HomeController has a constructor that takes a MyService instance as a parameter. This instance is stored as a private field and can be used throughout the controller. The Index action method also has a parameter of type MyService, which is decorated with the [FromServices] attribute. This indicates that the dependency injection container should resolve an instance of MyService and pass it to the action method when the action is called.

The [FromServices] attribute is useful because it allows you to easily inject dependencies into your action methods without having to manually resolve them or pass them as method parameters. This makes it easier to manage dependencies and makes your code more testable and maintainable.

In .NET MVC 6.0, the dependency injection container is provided by the IServiceProvider interface, which is implemented by the ServiceProvider class. To use the [FromServices] attribute, you must first register your dependencies with the dependency injection container. This can be done in the ConfigureServices method of the Startup class,

public void ConfigureServices(IServiceCollection services) {
    // Add your dependencies to the service collection
    services.AddTransient < MyService > ();
}

Once your dependencies are registered, you can use the [FromServices] attribute to inject them into your action methods.

The [FromServices] attribute can be removed in.NET 7,

//πŸ‘‡ No attribute required in .NET 7 
public IActionResult Index(MyService service) {
    // Use the service parameter to call a method on the service
    var result = service.DoSomething();
    return View(result);
}

Conclusion

I covered one of the updated MVC features in .NET 7 in this post. Many of the improvements are intended to make MVC and basic APIs more compatible with one another or to make it slightly simpler to utilize both in the same application.