ASP核心+ Angular应用。默认ASP DI容器。我正在尝试在ConfigureServices中使用Scrutor实现DI:
services.Scan(scan => scan.FromCallingAssembly().AddClasses().UsingRegistrationStrategy(RegistrationStrategy.Skip).AsSelf().WithTransientLifetime());
bool isThereClass11 = services.Any(x => x.ServiceType == typeof(Class11));它似乎起作用了: isThereClass11返回true。
接下来,我尝试在构造器中注入IClass11:public AuthController(IClass11 cl){}
因此,登录操作没有命中,我得到了"zone.js:2969 POST https://localhost:44362/api/auth/login 500“在前端。
public class Class11: IClass11 {}
public interface IClass11 {}如果AuthController构造函数没有参数,那么登录操作就会命中并按预期工作。我怎样才能在我的AuthController中注入依赖项,最好是使用巡查器(我不想在ConfigureServices中手动添加依赖项,也不想使用其他DI,除非我真的需要它)?
发布于 2019-02-26 00:53:08
您的services只知道Class11类型,但对IClass11接口一无所知。这是因为您使用AsSelf()方法注册了它。
那么,当你的控制器构造函数需要一个IClass11实现时,它就不能满足了。
如果您将使用AsSelfWithInterfaces()方法注册依赖项,那么它应该可以工作。
请参阅Source
https://stackoverflow.com/questions/54870626
复制相似问题