【发布时间】:2018-08-28 15:56:58
【问题描述】:
我正在创建帮助类以通过IServiceCollection 为库简化接口的配置和注入。库构造函数包含许多可能较早注入的依赖项。如果它们尚未插入IServiceCollection,则帮助程序类应添加它们。如何检测接口是否已经注入?
public static void AddClassLibrary(this IServiceCollection services
, IConfiguration configuration)
{
//Constructor for ClassLibrary requires LibraryConfig and IClass2 to be in place
//TODO: check IServiceCollection to see if IClass2 is already in the collection.
//if not, add call helper class to add IClass2 to collection.
//How do I check to see if IClass2 is already in the collection?
services.ConfigurePOCO<LibraryConfig>(configuration.GetSection("ConfigSection"));
services.AddScoped<IClass1, ClassLibrary>();
}
【问题讨论】:
-
services有哪些方法和属性可用?您需要使用智能感知进行检查。在 msdn 上关注IServiceCollection的文档
标签: c# dependency-injection .net-core