c# - 动态程序集加载时某些服务无法构建
问题描述
我正在尝试在我的 .net 核心项目(DNAAPI)中通过反射添加程序集。该外部项目 (EBiletManager) 包括 ILogger 和来自其他常见参考项目 (DNDomain.proj) 的其他所需服务。
public class EBiletManager : DNDomain.IScopedProcessingService {
ILogger _logger;
IConfiguration _configuration;
public EBiletManager(ILogger logger, IConfiguration configuration,
...
当我从项目依赖项中将 EBIletManager 添加到 DNAAPI 时,这些工作正常。
我花了几个小时解决这个问题,并尝试了这个插件方法。我尝试将所有 DLL 引用复制到 \bin\Debug\netcoreapp3.1 文件夹但不工作。
程序集加载正确,但“EBiletManager”构造器抛出此错误。
System.AggregateException:“某些服务无法构建(验证服务描述符时出错“ServiceType:DNDomain.Services.IScopedProcessingService Lifetime:Scoped ImplementationType:EBIletService.EBiletManager”:无法解析类型“Microsoft.Extensions.Logging”的服务.ILogger 尝试激活 'EBiletService.EBiletManager'。)'
1/2:InvalidOperationException:验证服务描述符“ServiceType:DNDomain.Services.IScopedProcessingService Lifetime:Scoped ImplementationType:EBiletService.EBiletManager”时出错:尝试激活“Microsoft.Extensions.Logging.ILogger”时无法解析服务类型EBiletService.EBiletManager'。
2/2:InvalidOperationException:尝试激活“EBiletService.EBiletManager”时无法解析“Microsoft.Extensions.Logging.ILogger”类型的服务。
服务描述行:
public static IServiceCollection AddCustomServices(this IServiceCollection services, IConfiguration configuration) {
services.AddHostedService<TimedHostedService>();
var loadContext = new PluginLoadContext(pluginLocation);
var assembly = loadContext.LoadFromAssemblyName(new AssemblyName(Path.GetFileNameWithoutExtension(pluginLocation)));
foreach (Type type in assembly.GetTypes()) {
if (type.GetInterface(nameof(DNDomain.IScopedProcessingService)) != null) {
services.AddScoped(typeof(DNDomain.IScopedProcessingService), type);
}
}
...
抱歉英语不好,感谢您的帮助。
解决方案
推荐阅读
- android - 如何从 JSON 中获取日期
- oracle - Oracle Cloud 和 Azure 之间的 VPN
- algorithm - 将 n 个值添加到空链表以便对其进行排序的时间复杂度?
- python - 如何不在python中获得重复值
- pandas - 如何避免从 DataFrame 到 CSV 的 unicode 错误?
- docker - docker compose up gving error as I need to stop mysql service ERROR: for demo_db_1 Cannot start
- python - NoReverseMatch at /login/ Reverse for 'teacher_dashboard' with arguments '({'teacher':object (2)>},)' 未找到。尝试了 1 种模式:
- docker - 层数是否对当前和未来 docker 镜像的大小、设置时间或性能有影响?
- android - 如果从 Google Play 商店下载 apk 和 obb,则无法在 android 10 设备上访问 obb 扩展文件
- python-3.x - 将 python2 混合类型 np.array 操作迁移到 python3