I created a custom IdentityErrorDescriber
based on this approach, and registered it like so:
services
.AddIdentityCore<CustomUser>()
.AddErrorDescriber<CustomIdentityErrorDescriber>()
// ...
I need an instance in a razor page model, which has this constructor:
public RegisterModel(CustomIdentityErrorDescriber errorDescriber, /* ... */)
{
// ...
}
However I get an exception when requesting that page:
System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.CustomIdentityErrorDescriber' while attempting to activate 'MyProject.Pages.RegisterModel'.
If I change the ctor to use the default IdentityErrorDescriber
type, then it works. Interestingly, it is actually an instance of CustomIdentityErrorDescriber
!
How can I resolve the custom type? Must I register it separately (in which case must I somehow deregister the default implementation from the container)?
Figured it out... it's a registration issue.
This:
.AddErrorDescriber<CustomIdentityErrorDescriber>()
Works like so:
public IdentityBuilder AddErrorDescriber<T>() where T : IdentityErrorDescriber
{
Services.AddScoped<IdentityErrorDescriber, T>();
return this;
}
So the solution is:
services
// ...
.AddErrorDescriber<CustomIdentityErrorDescriber>();
services.AddScoped<CustomIdentityErrorDescriber>();
Then the same describer object can be resolved as IdentityErrorDescriber
and CustomIdentityErrorDescriber
.