我正在尝试将接口和设置传递给一个类,同时在 .Net Core 项目的 startup.cs 中创建该类的实例。我正在使用下面的代码来做到这一点。我在 Student 类的构造函数中编写了一些代码,但是在启动应用程序时,构造函数中的代码/逻辑不起作用。没有错误,但调试指针没有命中 Student 类的构造函数。
services.AddSingleton(c => new Student(settings, resourceSetting, c.GetService<IPersonService>()));
如果我使用下面的代码,那么 Student 构造函数中的代码工作正常。
Student studentHandler = new Student(settings, resourceSetting,);
services.AddSingleton<Student>(studentHandler);
但是我需要在构造函数中传递服务接口才能在启动项目时做一些工作。谁能帮我解决我在这里缺少的东西?