Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 迪。将接口添加到AuthController构造函数后,不会命中登录操作_C#_Asp.net_Core - Fatal编程技术网

C# 迪。将接口添加到AuthController构造函数后,不会命中登录操作

C# 迪。将接口添加到AuthController构造函数后,不会命中登录操作,c#,asp.net,core,C#,Asp.net,Core,ASP核心+角度应用程序。默认的ASP DI容器。我正在尝试在ConfigureServices中实现DI: services.Scan(scan => scan.FromCallingAssembly().AddClasses().UsingRegistrationStrategy(RegistrationStrategy.Skip).AsSelf().WithTransientLifetime()); bool isThereClass11 = services.Any(x =>

ASP核心+角度应用程序。默认的ASP DI容器。我正在尝试在ConfigureServices中实现DI:

services.Scan(scan => scan.FromCallingAssembly().AddClasses().UsingRegistrationStrategy(RegistrationStrategy.Skip).AsSelf().WithTransientLifetime()); 
bool isThereClass11 = services.Any(x => x.ServiceType == typeof(Class11));
它似乎在起作用:IsThere Class11返回真值

接下来,我尝试将IClass11注入构造函数:
public AuthController(IClass11 cl){}

结果登录操作并没有命中,我在前端得到了“zone.js:2969 POST 500”

public class Class11: IClass11 {}
public interface IClass11 {}
如果AuthController构造函数没有参数,则登录操作将被命中并按预期工作。
如何最好使用Scrutor将依赖项注入AuthController(我不想在ConfigureServices中手动添加依赖项,也不想使用另一个DI,除非我真的需要它)

您的
服务
只知道
Class11
类型,但对
IClass11
接口一无所知。这是因为您使用
AsSelf()
方法注册了它

然后,当您的控制器构造函数期望一个
IClass11
实现时,它无法满足

如果改为使用
AsSelfWithInterfaces()
方法注册依赖项,那么它应该可以工作


请参见

您只声明
服务
知道
Class11
类型,而不知道
IClass11
(因为它是使用
AsSelf()注册的)。因此,当您的控制器期望IClass11实现时,不会提供它。您是否尝试改为使用
AsSelfWithInterfaces()
注册?@haim770谢谢!它起作用了,我真的需要更仔细地阅读[文档]()。请创建一个答案,以便我可以接受它。