.NET Core 类库中的依赖注入 [英] Dependency Injection in .NET Core inside a class library

查看:40
本文介绍了.NET Core 类库中的依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一个类注入到 .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 类库中的依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆