我正在try 在我的. Net 8应用程序中设置依赖注入,并希望它根据命名约定进行扫描和注册. 我遇到的问题是,我不知道如何注册IFoo<in T1, out T2>
个人创建的实现来设置对象的自定义映射.具体的实现继承自IFoo
的接口.
以下是目前为止我的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<object>(() => provider.GetService(@interface)));
}
}
}
下面是一个如何设置类的示例:
public interface ISpecificFoo : IFoo<Member, SpecificFoo.Output>
{
}
public class SpecificFoo : ISpecificFoo
{
public class Output
{
}
}
到目前为止,我得到的错误是:
System. ArgumentException:'无法实例化实现类型 "ISpecificFoo" 的针对服务类型 'IFoo'2