4

I have a class and generic interface, that when trying to inject the error occurs:

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);

    }
}

Depedency Injection

public void Register(IServiceCollection services)
{

   // Injection History Use Cases Application


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


}

Call the code that 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;
            }

        }
     }

}

Class that override method in 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);

            }
        }
    }
}

1 Answers1

1

You can't use generic types like this. AddTransient() expects that an instance of the second type specified can be assigned to a reference to the first type. A generic HandlerManager<> is not assignable to a IHandlerManager<>; you need to specify the implicit types and do so in a way that is consistent.

In addition, HandlerManager<T> is abstract and you can't create an instance of it.

Julia Hayward
  • 1,987
  • 1
  • 14
  • 16