首页 > 解决方案 > 动态程序集加载时某些服务无法构建

问题描述

我正在尝试在我的 .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);
                }
            }
...

抱歉英语不好,感谢您的帮助。

标签: c#asp.net-corereflectiondependency-injection

解决方案


推荐阅读