I'm trying to use dependency injection to add a generic service that has constructor parameters. I Need to implement this, generically:
host.Services.AddSingleton<IService>(x =>
new Service(x.GetRequiredService<IOtherService>(),
x.GetRequiredService<IAnotherOne>(),
""));
This is what I have working with open generics:
host.Services.AddSingleton(typeof(IGenericClass<>), typeof(GenericClass<>));
I have not been able to add constructor params with opengenerics. This is the class I want to add DI:
public class GenericClass<T> : IGenericClass<T> where T : class, new()
{
private readonly string _connectionString;
private readonly IGenericClass2<T> _anotherGenericInterface;
private readonly IInterface _anotherInterface;
public GenericClass(
string connectionString,
IGenericClass2<T> anotherGenericInterface,
IInterface anotherInterface)
{
_connectionString = connectionString ??
throw new ArgumentNullException(nameof(connectionString));
_executer = anotherGenericInterface;
_sproc = anotherInterface;
}
}