I have a Docker DotNet Core console app that processes messages from a queue.
There is a class called SimulationEngine
and I was expecting that ILogger
is passed by Dependency Injection automatically
public class SimulationEngine
{
ILogger<SimulationEngine> logger;
public SimulationEngine(ILogger<SimulationEngine> logger)
{
this.logger = logger;
}
This is how the instance is created:
public class RunsQueueProcessor : ...
{
public RunsQueueProcessor(..., ILogger<RunsQueueProcessor> logger)
{
}
protected override async Task ProcessMessage(...)
{
this.logger.LogInformation(...);
// Here DI is not working
var engine = new SimulationEngine();
DI works fine to inject logger into RunsQueueProcessor
, but it fails when I try to new SimulationEngine()
with the following error:
Error CS7036 There is no argument given that corresponds to the required formal parameter 'logger' of 'SimulationEngine.Simui lationEngine(ILogger<SimulationEngine>)'
How can I tell DotNet to use DI for that constructor?
--- EDIT ---
I wrote this question because I am working on a PoC and I don't need Dependency Injection everywhere. I only need it in few places where I am doing Unit Testing to validate specific algorithms.
A more concrete questions is if there is a way to configure DotNet DI framework to inject dependencies when using the new
statement or manually instantiating objects.
Thanks to all comments and answers, Now I understand more about how DotNet DI works.