【发布时间】:2017-03-28 07:22:43
【问题描述】:
我有AuthenticationStrategy 类,我将在控制器构造函数中注入它。
我有两个IAuthenticationProviders:InternalAuthenticationProvider 和 ExternalAuthenticationProvider。
在AuthenticationStrategy 构造函数中,我想注入所有提供者。
示例代码:
public class AuthenticationStrategy
{
private readonly Dictionary<string, IAuthenticationProvider> _authenticationProviders;
public AuthenticationStrategy(IAuthenticationProvider[] authenticationProviders)
{
if (authenticationProviders == null)
{
throw new ArgumentNullException("AuthenticationProviders");
}
_authenticationProviders = authenticationProviders
.ToDictionary(x => nameof(x), x => x);
}
}
如何使用依赖注入注入多个提供程序? 示例代码:
services.AddScoped<IAuthenticationProvider, InternalAuthenticationProvider>();
services.AddScoped<IAuthenticationProvider, ExternalAuthenticationProvider>();
services.AddScoped<AuthenticationStrategy>();
有什么想法吗?
【问题讨论】:
-
您是否遇到任何错误?当您尝试上述代码时会发生什么?
-
IAuthenticationProvider[] authenticationProviders 在 AuthenticationStrategy 构造函数中为空。
标签: c# .net dependency-injection constructor .net-core