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?
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 .
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.
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).
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 ...
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With