2017-05-14 59 views
0

我想在Stormpath註冊後將新用戶添加到我的本地數據庫。在文檔https://docs.stormpath.com/dotnet/aspnetcore/latest/registration.html#registration是關於註冊後處理程序的部分。我有問題,因爲我不能在StartUp文件中使用UserRepository。 我有錯誤:在Stormpath註冊後將用戶添加到本地數據庫

Unable to resolve service for type 'AppProject.Repositories.IUserRepository' while attempting to activate '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); 
     } 
+1

重要提醒Stormpath將於2017年8月17日中午PST關閉。如果您還沒有,請開始遷移到另一項服務。我推薦Okta的開發者平臺。 – Alex

回答

0

在這種情況下,我不認爲它可以解決安裝調試IUserRepository的依賴。你可以嘗試這樣的事情。

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輸入參數。