Dependency injection 依赖项注入容器未向构造函数提供contextAccessor参数 初始问题
我正在调用此Dependency injection 依赖项注入容器未向构造函数提供contextAccessor参数 初始问题,dependency-injection,asp.net-core,asp.net-identity-2,Dependency Injection,Asp.net Core,Asp.net Identity 2,我正在调用此serviceProvider.GetRequiredService(),并收到以下错误。为什么依赖注入不负责提供非空的上下文访问器 System.ArgumentNullException was unhandled by user code HResult=-2147467261 Message=Value cannot be null. Parameter name: contextAccessor ParamName=contextAccessor Sourc
serviceProvider.GetRequiredService()
,并收到以下错误。为什么依赖注入不负责提供非空的上下文访问器
System.ArgumentNullException was unhandled by user code
HResult=-2147467261
Message=Value cannot be null.
Parameter name: contextAccessor
ParamName=contextAccessor
Source=Microsoft.AspNet.Identity
StackTrace:
at Microsoft.AspNet.Identity.SignInManager`1..ctor(UserManager`1 userManager, IHttpContextAccessor contextAccessor, IUserClaimsPrincipalFactory`1 claimsFactory, IOptions`1 optionsAccessor, ILogger`1 logger)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.Framework.DependencyInjection.ServiceLookup.Service.ConstructorCallSite.Invoke(ServiceProvider provider)
at Microsoft.Framework.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
at Microsoft.Framework.DependencyInjection.ServiceProvider.<>c__DisplayClass8_0.<RealizeService>b__0(ServiceProvider provider)
at Microsoft.Framework.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.Framework.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Framework.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
at ResourceOwnerPasswordFlow.Startup.<>c__DisplayClass6_0.<Configure>b__1(OpenIdConnectServerOptions options) in C:\Users\BigFont\Documents\GitHub\AspNet.Security.OpenIdConnect.Server\samples\ResourceOwnerPasswordFlow\Startup.cs:line 109
at Microsoft.Framework.OptionsModel.ConfigureOptions`1.Configure(TOptions options, String name)
at Microsoft.AspNet.Authentication.AuthenticationMiddleware`1..ctor(RequestDelegate next, IOptions`1 options, ILoggerFactory loggerFactory, IUrlEncoder encoder, ConfigureOptions`1 configureOptions)
at AspNet.Security.OpenIdConnect.Server.OpenIdConnectServerMiddleware..ctor(RequestDelegate next, ILoggerFactory loggerFactory, IDistributedCache cache, IHtmlEncoder htmlEncoder, IUrlEncoder urlEncoder, IDataProtectionProvider dataProtectionProvider, IOptions`1 options, ConfigureOptions`1 configuration) in C:\Users\BigFont\Documents\GitHub\AspNet.Security.OpenIdConnect.Server\src\AspNet.Security.OpenIdConnect.Server\OpenIdConnectServerMiddleware.cs:line 38
InnerException:
serviceProvider.GetRequiredServiceserviceProvider.GetRequiredService< SignInManager< ApplicationUser>>()
而且,IHttpContextAccessor
确实被注入,而我不必显式地将其添加到服务容器中。然后我可以稍后使用它访问当前的httpContext
,如下所示:
string host = contextAccessor.HttpContext.Request.Host.Value;
在我自己的代码中,我使用的是
IdentityUser
,而不是像ApplicationUser
这样的扩展;相反,它来自文档中的一个代码示例。我不确定服务定位器是否是反模式的。这似乎是我们应该深思熟虑地使用的东西。
serviceProvider.GetRequiredService< SignInManager< ApplicationUser>>()
public RequestSiteResolver(
ISiteRepository siteRepository,
IConfiguration configuration,
IHttpContextAccessor httpContextAccessor)
{
contextAccessor = httpContextAccessor;
siteRepo = siteRepository;
config = configuration;
}
string host = contextAccessor.HttpContext.Request.Host.Value;