C#ILogger依赖项注入错误:没有参数
我有一个Docker DotNet Core控制台应用程序,可以处理队列中的消息 有一个名为C#ILogger依赖项注入错误:没有参数,c#,.net-core,dependency-injection,C#,.net Core,Dependency Injection,我有一个Docker DotNet Core控制台应用程序,可以处理队列中的消息 有一个名为SimulationEngine的类,我希望ILogger通过依赖注入自动传递 public class SimulationEngine { ILogger<SimulationEngine> logger; public SimulationEngine(ILogger<SimulationEngine> logger) { this.
SimulationEngine
的类,我希望ILogger
通过依赖注入自动传递
public class SimulationEngine
{
ILogger<SimulationEngine> logger;
public SimulationEngine(ILogger<SimulationEngine> logger)
{
this.logger = logger;
}
我怎样才能告诉DotNet为该构造函数使用DI
---编辑---
我写这个问题是因为我正在研究PoC,我不需要到处都进行依赖注入。我只需要在我正在进行单元测试以验证特定算法的少数地方使用它。
一个更具体的问题是,在使用new
语句或手动实例化对象时,是否有方法配置dotnetdi框架以注入依赖项
感谢所有的评论和回答,现在我对DotNet DI的工作原理有了更多的了解。根据您上次的评论,您希望更多地关注于解释依赖项注入:另一个选项,这是一种传统且正确的方法:
ISimulationEngine
SimulationEngine
IServiceCollection
中的接口(在startup.cs中)runsqueprocessor
类的构造函数中注入it
,以便应用程序理解您想要使用SimulationEngine
public interface ISimulationEngine
{
void DoSomthing();
}
public class SimulationEngine : ISimulationEngine
{
private readonly ILogger<SimulationEngine> _logger;
public SimulationEngine(ILogger<SimulationEngine> logger) //<-- here you are injecting the registered Singleton of ILogger
{
_logger = logger;
}
public async void DoSomthing()
{
throw new NotImplementedException();
}
}
public class AnotherService: ISimulationEngine
{
public async void DoSomthing()
{
throw new NotImplementedException();
}
}
实现接口的服务
public interface ISimulationEngine
{
void DoSomthing();
}
public class SimulationEngine : ISimulationEngine
{
private readonly ILogger<SimulationEngine> _logger;
public SimulationEngine(ILogger<SimulationEngine> logger) //<-- here you are injecting the registered Singleton of ILogger
{
_logger = logger;
}
public async void DoSomthing()
{
throw new NotImplementedException();
}
}
public class AnotherService: ISimulationEngine
{
public async void DoSomthing()
{
throw new NotImplementedException();
}
}
在startup.cs中注册服务
public void ConfigureServices(IServiceCollection services)
{
//...
// this means whenever ISimulationEngine injected that class can use
// SimulationEngine service
services.AddTransient<ISimulationEngine, SimulationEngine>();
//...
}
作为结论:
您可以想象注入的过程,比如类say去检查DI容器,哪个服务是为这个接口定义的?!让我用它
您可能会问AdTransient和AddSingleton之间有什么不同(也有AddScope)检查链接根据您最后的评论,您希望更多地关注于解释依赖项注入:另一个选项是:
ISimulationEngine
SimulationEngine
IServiceCollection
中的接口(在startup.cs中)runsqueprocessor
类的构造函数中注入it
,以便应用程序理解您想要使用SimulationEngine
public interface ISimulationEngine
{
void DoSomthing();
}
public class SimulationEngine : ISimulationEngine
{
private readonly ILogger<SimulationEngine> _logger;
public SimulationEngine(ILogger<SimulationEngine> logger) //<-- here you are injecting the registered Singleton of ILogger
{
_logger = logger;
}
public async void DoSomthing()
{
throw new NotImplementedException();
}
}
public class AnotherService: ISimulationEngine
{
public async void DoSomthing()
{
throw new NotImplementedException();
}
}
实现接口的服务
public interface ISimulationEngine
{
void DoSomthing();
}
public class SimulationEngine : ISimulationEngine
{
private readonly ILogger<SimulationEngine> _logger;
public SimulationEngine(ILogger<SimulationEngine> logger) //<-- here you are injecting the registered Singleton of ILogger
{
_logger = logger;
}
public async void DoSomthing()
{
throw new NotImplementedException();
}
}
public class AnotherService: ISimulationEngine
{
public async void DoSomthing()
{
throw new NotImplementedException();
}
}
在startup.cs中注册服务
public void ConfigureServices(IServiceCollection services)
{
//...
// this means whenever ISimulationEngine injected that class can use
// SimulationEngine service
services.AddTransient<ISimulationEngine, SimulationEngine>();
//...
}
作为结论:
您可以想象注入的过程,比如类say去检查DI容器,哪个服务是为这个接口定义的?!让我用它
您可能会问AdTransient和AddSingleton之间有什么不同(也有AddScope)检查链接
new SimulationEngine()。您需要在您的新
语句中提供记录器,或者从您的服务集合中请求一个实例如果您没有将DI用于模拟引擎
,则需要向构造函数添加一个参数,例如:公共RunsqueProcessor(…,ILogger,SimulationEngine)
谢谢,我的问题更多的是“何时可以,何时不能使用DI”。我曾期望dotNet能够在任何地方都能做到这一点,似乎它只适用于ASP控制器之类的东西。是吗?你误解了DI是什么,我建议你去读一读。定义一个接口ISimulationEngine并实现SimulationEngine,然后将ISimulationEngine注入你的处理器new SimulationEngine()
没有得到DI的好处,你的新操作是手动的。您需要在您的新
语句中提供记录器,或者从您的服务集合中请求一个实例如果您没有将DI用于模拟引擎
,则需要向构造函数添加一个参数,例如:公共RunsqueProcessor(…,ILogger,SimulationEngine)
谢谢,我的问题更多的是“何时可以,何时不能使用DI”。我曾期望dotNet能够在任何地方都能做到这一点,似乎它只适用于ASP控制器之类的东西。是吗?你误解了DI是什么,我建议你去读一下。定义一个接口ISimulationEngine并实现SimulationEngine,然后将ISimulationEngine注入到你的进程中。非常感谢@U8080,非常清楚!最后一个问题。为什么DotNet在我的示例中不注入ILogger,但在您的示例中却注入ISimulationEngine?或者为什么它在RunsqueProcessor中注入ILogger而不是在SimulationEngine中?谢谢@Carlos Garcia,很高兴听到它对您提出的问题很有用,因为您使用的是DotNet Core的默认ILogger,它已经注册,设计用于泛型类型ILogger,这意味着您需要清楚地提到该类。另一个选项是使用第三方记录器,如SeriLog,它可以在不提及泛型类型的情况下使用。结果是:它是如何设计的。@Carlos Garcia,如果你问为什么在C#中使用DI,答案是很多好处,但最重要的是解耦(消除使用new
关键字)new
关键字使消费者类依赖于服务器类。我想我现在明白了,DotNet Core中的DI仅在框架实例化类时起作用。当您使用new
手动实例化该类时,它不会执行此操作。谢谢@U8080的帮助!!现在我了解了它在C中的工作原理:)非常感谢@U8080,非常清楚!最后一个问题。为什么DotNet在我的示例中不注入ILogger,但在您的示例中却注入ISimulationEngine?或者它为什么要把伊洛格注射进去