小编Dan*_*ney的帖子

如何确定服务是否已添加到IServiceCollection中

我正在创建帮助程序类,以简化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>();
}
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection .net-core

15
推荐指数
1
解决办法
3187
查看次数

标签 统计

.net-core ×1

c# ×1

dependency-injection ×1