0

Hello i am trying to inject dependency of service with multiples implementations (multiple classes that implement the IDBActionQuery interface). I have this code in my Program.cs

builder.Services.AddTransient<IDBActionQuery<Period>, Select<Period>>();

And i have this interface:

public interface IDBActionQuery<T>
    {
        Task<List<T>> execute(string query, params (string name, object val)[] pars);
        Task<List<T>> buildAndExecute();
    }

And this is my Select class:

public class Select<T> : DBMotorBase<T>, IDBActionQuery<T>
    {
        private readonly IPrintExceptions exceptions;
        private readonly IGetPropertiesService propertiesService;
        private readonly ISQLParametersService sqlParameterService;
        private readonly ISerializeService serializeService;
        private readonly IDeserealizeService<T> deserealizeService;

        public Select(string _connection,
            IPrintExceptions _exceptions, IGetPropertiesService _propertiesService,
            ISQLParametersService _sqlParameterService, ISerializeService _serializeService,
            IDeserealizeService<T> _deserealizeService) : base(_connection)
        {
            exceptions = _exceptions;
            propertiesService = _propertiesService;
            sqlParameterService = _sqlParameterService;
            serializeService = _serializeService;
            deserealizeService = _deserealizeService;
        }

        private string build(string schema = "dbo")
        {
            ...
        }
        public async Task<List<T>> buildAndExecute()
        {
            return await execute(build(),null);
        }

        public async Task<List<T>> execute(string query, params (string name, object val)[] pars)
        {
            ...
        }

        private async Task<List<T>> processCommand(SqlConnection sql, 
            string query, params (string name, object val)[] pars)
        {
            ...
        }
        private async Task<List<T>> processReader(SqlCommand cmd)
        {
          ....
        }
    }

However i am receiving this error:

Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType:

Why is that???

sofnomic cr
  • 132
  • 1
  • 9

1 Answers1

1

The constructor of Select<T> should only contain Classes/Interfaces that can be injected. To do that, they must be registered as services like you did with Select<T>.

This is why it's throwing the Some services are not able to be constructed. It can't resolve the services you specified in the constructor. It can't create a fitting instance.

Guessing from your code, the problem is "string _connection". Try injecting "string _connection" via the e.g. IConfiguration or IOptions pattern instead. (Of course the other constructor parameters need to be registered services too!)

Something like this:

appsettings.json:

"ConnectionOptions": {
    "Connection": "..."
},

Program.cs:

builder.Services.Configure<ConnectionOptions>
    (builder.Configuration.GetSection(nameof(ConnectionOptions)));

Select<T>:

public Select(IOptions<ConnectionOptions> options,
              IPrintExceptions exceptions, 
              IGetPropertiesService propertiesService,
              ISQLParametersService sqlParameterService, 
              ISerializeService serializeService,
              IDeserealizeService<T> deserealizeService)
              : base(options.Connection)
        {
            _exceptions = exceptions;
            _propertiesService = propertiesService;
            _sqlParameterService = sqlParameterService;
            _serializeService = serializeService;
            _deserealizeService = deserealizeService;
        }

If the "string _connection" property is a regular db connection string, I'd use IConfiguration instead.

Check out this question and similar ones for some detailed explanations.

M-Expunged
  • 71
  • 1
  • 6