Вот мой код DI:
Код: Выделить всё
public static void ConfigureServices(IServiceCollection services, Assembly assembly)
{
var types = assembly.GetTypes()
.Where(t => t.GetInterfaces().Any(i => i.IsGenericType)).ToList();
foreach (var type in types)
{
var interfaces = type.GetInterfaces();
foreach (var @interface in interfaces)
{
// Register the interface mapping to the type in DI container
services.AddTransient(@interface, type);
// Register a factory for the Funcs
services.AddTransient(typeof(Func).MakeGenericType(@interface),
provider => new Func(() => provider.GetService(@interface)));
}
}
}
Код: Выделить всё
public interface ISpecificFoo : IFoo
{
}
public class SpecificFoo : ISpecificFoo
{
public class Output
{
}
}
System.ArgumentException: 'Невозможно создать экземпляр типа реализации
'ISpecificFoo'
для типа службы
'IFoo`2
Источник: https://stackoverflow.com/questions/781 ... etype-type