c# - 在泛型类中注入(inject)依赖项时出错

标签 c# dependency-injection

我有一个类和通用接口(interface),当尝试注入(inject)时发生错误:

System.ArgumentException: 'Cannot instantiate implementation type 'Application.Process.ProcessIntegrationUseCase.GenericClass.HandlerManager1[T]' for service type 'Application.Process.ProcessIntegrationUseCase.GenericClass.IHandlerManager1[T]'.'

namespace Application.Process.ProcessIntegrationUseCase.GenericClass
{
    public abstract class HandlerManager<T>: IHandlerManager<T> 
    {
        protected IHandlerManager<T> sucessor;

        public void SetSucessor(IHandlerManager<T> sucessor)
        {
            this.sucessor = sucessor;

        }

        public abstract void ProcessRequest(T request);
     }
}

接口(interface) IHandlerManager

namespace Application.Process.ProcessIntegrationUseCase.GenericClass
{
    public interface IHandlerManager<T> 
    {
        void SetSucessor(IHandlerManager<T> sucessor);

        void ProcessRequest(T request);

    }
}

依赖注入(inject)

public void Register(IServiceCollection services)
{

   // Injection History Use Cases Application


   services.AddTransient(typeof(IHandlerManager<>),
   typeof(HandlerManager<>));


}

调用注入(inject)HandlerManager的代码

using Domain.Meta;
using System;
using Microsoft.Extensions.Logging;
using Application.Process.ProcessIntegrationUseCase.GenericClass;

namespace Application.UseCases.Process.ProcessIntegrationUseCase.Habitacional
{
    public sealed class ProcessHabitacionalUseCase : IProcessHabitacionalUseCase
    {
        private readonly StartProcessHandler<HistoryHabitacional> _startProcessHandler;

        private readonly ILogger _iLogger;

        public ProcessHabitacionalUseCase(ILogger iLogger,
                                    StartProcessHandler<HistoryHabitacional> startProcessHandler)

        {
            _iLogger = iLogger;

            _startProcessHandler = startProcessHandler;

        }

        public void Execute(HistoryHabitacional history)
        {
            if (history == null)
                throw new ArgumentNullException();

            try
            {              

               _startProcessHandler.ProcessRequest(history);

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
     }

}

在 HandlerManager 中覆盖方法的类

using System;
using System.Reflection;
using Microsoft.Extensions.Logging;
using Application.Repositories.History;
using Application.Repositories.I4Pro;
using Domain.Process.Enum;

namespace Application.Process.ProcessIntegrationUseCase.GenericClass
{

     public class StartProcessHandler<T> : HandlerManager<T> where T: class
    {
        private readonly ILogger _iLogger;
        private readonly IHistoryReadOnlyRepository _historyReadOnlyRepository;
        private readonly II4ProReadOnlyRepository _i4ProReadOnlyRepository;

         public StartProcessHandler(ILogger iLogger,
                               IHistoryReadOnlyRepository historyReadOnlyRepository,
                               II4ProReadOnlyRepository i4ProReadOnlyRepository)
        {
             _iLogger = iLogger;
             _historyReadOnlyRepository = historyReadOnlyRepository;
             _i4ProReadOnlyRepository = i4ProReadOnlyRepository;
         }

        public override void ProcessRequest(T history)
        {
            try
            {

                 TypeIntegration typeIntegration = (TypeIntegration)history.GetType().GetProperty("TypeIntegration").GetValue(history);

                _iLogger.LogInformation("Buscando execuções MetaIntegra");
                var item = _historyReadOnlyRepository.GetLastHistory(typeIntegration);

                _iLogger.LogInformation("Buscando execuções I4Pro");
                var i4Process = _i4ProReadOnlyRepository.ListLastExecutions();

                _iLogger.LogInformation("Executing Habitacional Integration_" + DateTime.Now.ToString());
                 if ((item != null && i4Process[0].Id_Processo_Log != item.LastIdI4Pro) || item == null)
                 { 
     history.GetType().GetProperty("LastIdI4Pro").SetValue(history, 
item.LastIdI4Pro);

     history.GetType().GetProperty("IdProcessoLog").SetValue(history, 
i4Process[0].Id_Processo_Log);

                    if (base.sucessor != null)
                    sucessor.ProcessRequest(history);

                }
            }
            catch (Exception ex)
            {
                _iLogger.LogError(ex.Message);

            }
        }
    }
}

最佳答案

你不能像这样使用通用类型。 AddTransient()期望可以将指定的第二种类型的实例分配给对第一种类型的引用。通用 HandlerManager<>不可分配给 IHandlerManager<> ;您需要以一致的方式指定隐式类型。

此外,HandlerManager<T>是抽象的,您不能创建它的实例。

关于c# - 在泛型类中注入(inject)依赖项时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56772122/

相关文章:

c# - 使用不同类型访问的 Entity Framework 性能

c# - 是否有用于解析/格式化 TCP 数据包的库或任何有助于这样做的库?

c# - 从 Web 应用程序使用 photoshop 文件

c# - 无法将类型为 '%0' 的实例添加到 Windows Phone 8.1 DatePicker 中类型为 '%1' 的集合中

c# - CSS 文本区域未填充父项

c# - 如何使用 Autofac 解决属性注入(inject)问题?

java - Spring Boot 具有更多上下文

java - 为每个租户自定义 Spring @Scope

c++ - 如何将shared_ptr<void>转换为另一种类型?

c# - 通过 NDepend 查找仅在特殊类中使用的代码