Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET 5 Dependency Injection - Does the [FromServices] attribute only work in a controller?

Using asp.net 5 beta-8, I have something like this registered as a service:

services.AddTransient<IMyClass, MyClass>();

When I use this attribute on a property in a controller, myClass gets set to the instance of the IMyClass.

public class HomeController : Controller
{
    [FromServices]
    public IMyClass myClass { get; set; }

    public IActionResult Index()
    {
        //myClass is good
        return View();
    }
}

However, when I try to use it in a class which doesn't inherit Controller, it doesn't seem to set the myClass property to an instance of IMyClass:

public class MyService
{
    [FromServices]
    public IMyClass myClass { get; set; }

    public void DoSomething(){
      //myClass is null
    }
}

Is this the expected behavior? How do I inject my dependencies in a regular class?

like image 554
Y.Z. Avatar asked Nov 05 '15 02:11

Y.Z.


People also ask

Does ASP NET Core support dependency injection?

ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. For more information specific to dependency injection within MVC controllers, see Dependency injection into controllers in ASP.NET Core .

Is it possible to inject dependencies directly into the action method?

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.

What is dependency injection in MVC?

Dependency Injection (DI) in MVC Dependency Injection is an implementation of "Inversion of Control". Inversion of Control (IoC) says that the objects do not create other objects on which they rely to do their work; instead, they get the objects that they need from an outside source (for example, an XML configuration file).

What are the methods of dependency injection into Controllers in core?

Dependency injection into controllers in ASP.NET Core 1 Constructor Injection. Services are added as a constructor parameter, and the runtime resolves the service from the... 2 Action injection with FromServices. 3 Access settings from a controller. Accessing app or configuration settings from within a controller is a common pattern. More ...


1 Answers

The problem is when you call new MyService(), the ASP.NET 5 dependency injection system is totally bypassed.

In order to have dependencies injected into MyService, we need to let ASP.NET create the instance for us.

If you want to use MyService in your controller, you can have to first register it with the services collection along with IMyClass.

services.AddTransient<IMyClass, MyClass>();
services.AddTransient<MyService>();

In this case, I chose to use constructor injection so I don't make the mistake of trying to instantiate MyService myself:

public class MyService
{
    public IMyClass myClass { get; set; }

    public MyService(IMyClass myClass)
    {
        this.myClass = myClass;
    }

    public void DoSomething()
    {
        //myClass is null
    }
}

Now you are free to inject this service into your controller:

public class HomeController : Controller
{
    [FromServices]
    public MyService myService { get; set; }

    public IActionResult Index()
    {
        // myService is not null, and it will have a IMyClass injected properly for you
        return View();
    }
}

If you want to learn more about the ASP.NET 5 dependency injection system, I made a video and blog post here: http://dotnetliberty.com/index.php/2015/10/15/asp-net-5-mvc6-dependency-injection-in-6-steps/

like image 69
armen.shimoon Avatar answered Oct 03 '22 00:10

armen.shimoon