Добавить пользователя в локальную базу данных после регистрации в Stormpath

Я хочу добавить нового пользователя в свою локальную базу данных после регистрации в Stormpath. В документе https://docs.stormpath.com/dotnet/aspnetcore/latest/registration.html#registration — раздел об обработчике пострегистрации. У меня проблема, потому что я не могу использовать UserRepository в файле StartUp. у меня ошибка:

Не удалось разрешить службу для типа «AppProject.Repositories.IUserRepository» при попытке активировать «AppProject.Startup»


.

    public void ConfigureServices(IServiceCollection services, IUserRepository userRepository)
            {
    services.AddStormpath(new StormpathOptions()
               {
                Configuration = new StormpathConfiguration()
                {
                    Client = new ClientConfiguration()
                    {
                        ApiKey = new ClientApiKeyConfiguration()
                        {
                            Id = "xxxxxxxxxxx",
                            Secret = "xxxxxxxxx"
                        }
                    }
                },
                PostRegistrationHandler = (context, ct) =>
               {
                   return MyPostRegistrationHandler(context, ct, userRepository);
               }
            });
}



   private Task MyPostRegistrationHandler(PostRegistrationContext context, CancellationToken ct, IUserRepository userRepository)
        {
            userRepository.Add(new User(context.Account.Email, context.Account.FullName, context.Account.GivenName, context.Account.Surname, context.Account.Username));
            userRepository.SaveChangesAsync();
            return Task.FromResult(0);
        }

person Karolina Szczepaniak    schedule 14.05.2017    source источник
comment
Важное напоминание о том, что Stormpath закрывается 17 августа 2017 г. в полдень по тихоокеанскому стандартному времени. Если вы еще этого не сделали, начните переход с другой службы на другую. Я рекомендую платформу разработчика Okta.   -  person Alex    schedule 15.05.2017


Ответы (1)


В этом сценарии я не думаю, что он может разрешить зависимость IUserRepository в StartUp. Вы можете попробовать что-то вроде этого.

1) Добавьте метод расширения.

public static IServiceProvider AddServices(this IServiceCollection services)
{
    services.AddTransient<IUserRepository, UserRepository>();
    // rest of the things.
    return services.BuildServiceProvider();
}

2) Получите экземпляр userRepository, подобный этому.

   IServiceCollection services = new ServiceCollection();
   services.AddServices();
   var provider = services.BuildServiceProvider();
   var userRepository = provider.GetRequiredService<IUserRepository>();

ConfigurationServices не будет иметь входного параметра IUserRepository.

person Raj Karri    schedule 14.05.2017