Initial Question
I'm calling this serviceProvider.GetRequiredService<SignInManager<IdentityUser>>()
and am receiving the following error. Why doesn't the dependency inject take care of supplying a non-null contextAccessor
?
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:
Research
I've seen the constructor injection done in the sample project here and it just works. What am I missing?
public ManageController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager)
{
UserManager = userManager;
SignInManager = signInManager;
}
I've also seen in the Music Store Tests the explicit addition of an HttpContextAccessor
into the service collection. Do I need to do this also? How come?
// IHttpContextAccessor is required for SignInManager, and UserManager
services.AddInstance<IHttpContextAccessor>(
new HttpContextAccessor()
{
HttpContext = new TestHttpContext(),
});