问题描述
如何将一个类注入到 .NET Core 库项目中的另一个类中?我应该在哪里配置 DI,因为它是在 API 项目中的 StartUp Class ConfigureServices 中完成的?
How can I inject one class into another inside a .NET Core library project?Where should I configure DI as it is done in StartUp Class ConfigureServices in API project?
推荐答案
关于如何管理这个问题有很多思考过程,因为最终调用者需要为你注册你的 DI 过程.
There are many thought processes for how you manage this, as eventually, the caller will need to register your DI processes for you.
如果您查看 Microsoft 和其他公司使用的方法,您通常会使用诸如AddMyCustomLibrary"之类的方法定义一个扩展方法作为 IServiceCollection 的扩展方法.这里有一些讨论.
If you look at the methods used by Microsoft and others, you will typically have an extension method defined with a method such as "AddMyCustomLibrary" as an extension method off of the IServiceCollection. There is some discussion on this here.
这篇关于.NET Core 类库中的依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!