【问题标题】:Error to inject dependency in generic class在泛型类中注入依赖项时出错
【发布时间】:2019-06-26 11:50:01
【问题描述】:

我有一个类和通用接口,当尝试注入错误时:

System.ArgumentException: '无法实例化实现类型 '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);
     }
}

接口 IHandlerManager

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

        void ProcessRequest(T request);

    }
}

依赖注入

public void Register(IServiceCollection services)
{

   // Injection History Use Cases Application


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


}

调用注入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);

            }
        }
    }
}

【问题讨论】:

标签: c# dependency-injection


【解决方案1】:

您不能使用这样的泛型类型。 AddTransient() 期望可以将指定的第二种类型的实例分配给对第一种类型的引用。通用HandlerManager&lt;&gt; 不能分配给IHandlerManager&lt;&gt;;您需要指定隐式类型并以一致的方式执行此操作。

另外,HandlerManager&lt;T&gt; 是抽象的,你不能创建它的实例。

【讨论】:

    猜你喜欢
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 2022-12-04
    相关资源
    最近更新 更多