Daarto
Daarto copied to clipboard
IdentityBuilderExtensions.AddStores ignores AddUsersTable option if implementation is UserOnlyStore
DapperStoreOptionsExtension adds the scoped IService
options.Services.AddScoped(typeof(IUsersTable<,,,,,>).MakeGeneric....
However if a roleType is not provided, DI looks for an implementation of IUsersOnlyTable<,,,,>
. There are 2 possible solutions depending on what you want:
- add a scoped service for both
IUsersTable
andIUsersOnlyTable
in theAddUsersTable
method ofDapperStoreOptionsExtension
- Alter the IServicesCollection to replace the IService for
IUsersTable
with 1 for the serviceIUsersOnlyTable
and the same implementationType, along the lines of:
var usersTableServiceType = typeof(IUsersTable<,,,,,>).MakeGenericType(userType, keyType, userClaimType, userRoleType, userLoginType, userTokenType);
if (roleType != null) {
...
services.TryAddScoped(
usersTableServiceType,
typeof(UsersTable<,,,,,>).MakeGenericType(userType, keyType, userClaimType, userRoleType, userLoginType, userTokenType)
);
....
} else {
var serviceType = services.FirstOrDefault(s => s.ServiceType == usersTableServiceType);
usersTableServiceType = typeof(IUsersOnlyTable<,,,,>).MakeGenericType(userType, keyType, userClaimType, userLoginType, userTokenType);
if (serviceType == null)
{
services.TryAddScoped(usersTableServiceType, typeof(UsersTable<,,,,,>).MakeGenericType(userType, keyType, userClaimType, userRoleType, userLoginType, userTokenType));
}
else
{
services.Remove(serviceType);
services.TryAddScoped(usersTableServiceType, serviceType.ImplementationType);
}
userStoreType = typeof(UserOnlyStore<,,,,>).MakeGenericType(userType, keyType, userClaimType, userLoginType, userTokenType);
}