问题
我有许多具有两个类型参数的具体泛型类,它们实现了具有一个类型参数的泛型接口。例如:
public interface ISomeService<T>
{
// ...
}
public class SomeService<TA, TB> : ISomeService<TA>
{
// ...
}
我像这样使用 Autofac 注册它们:
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterGeneric(typeof(SomeService<,>))
.As(typeof(ISomeService<>))
.InstancePerLifetimeScope();
var container = containerBuilder.Build();
当尝试解决这样的实例时ISomeService<Foo>:
var service = container.Resolve<ISomeService<Foo>>();
我收到一个Autofac.Core.Registration.ComponentNotRegisteredException异常,说请求的服务ISomeService`1[[Foo]]尚未注册。
问题
- 我想用 Autofac 做的事情是不可能的吗?
- 如果是这样,是否有解决方法?
- 如果没有,其他 DI 容器是否提供这种能力,例如 SimpleInjector?