SimpleInjector icon indicating copy to clipboard operation
SimpleInjector copied to clipboard

Registering IUserStore with Simple Injector 3.1.2

Open oliverslade opened this issue 10 years ago • 2 comments

Hi I have been trying to register the default IUserStore that is in the premade Identity class ApplicationUserManager with SimpleInjector

public class ApplicationUserManager : UserManager<ApplicationUser, int>
    {
        public ApplicationUserManager(IUserStore<ApplicationUser, int> store)
            : base(store)
        {
        }

I have a SimpleInjectorInitializer file in the App_Start folder that looks like this:

public static class SimpleInjectorInitializer
{
    public static void RegisterDependencies(Container container)
    {
        container.Register<ICatalogRepository, CatalogRepository>();
        container.Register<IUserRepository, UserRepository>();

        container.Register<ICatalogService, CatalogService>();
        container.Register<IUserService, UserService>();
        container.Register<IAdminService, AdminService>();

        //container.Register<AccountController>();
        //container.Register<ApplicationUserManager>();
        //container.Register<CustomUserStore>();
        //container.RegisterCollection(typeof(IUserStore<ApplicationUser, int>), new[] { typeof(CustomUserStore) });
        //container.RegisterPerWebRequest<IUserStore<ApplicationUser>>(() => new UserStore<ApplicationUser>(container.GetInstance<ApplicationDbContext>()));

        container.Verify();
    }
}

As you can see I have tried a few times to get it working by looking at similar Stack Overflow questions but it has either not changed the outcome or given a syntax error.

Currently when clicking the sign in button on the application homepage, this error is thrown:

The constructor of type ApplicationUserManager contains the parameter with name 'store' and type IUserStore that is not registered. Please ensure IUserStore is registered, or change the constructor of ApplicationUserManager.

If the two lines below register ApplicationUserManager are uncommented then this error is thrown:

An exception of type 'System.ArgumentException' occurred in SimpleInjector.dll but was not handled in user code

Additional information: The supplied type ApplicationUser does not implement IUserStore.

Any help on this would be appreciated.

oliverslade avatar Mar 29 '16 22:03 oliverslade

I seem to have fixed this issue by changing my Initializer to:

public static void RegisterDependencies(Container container)
        {
            container.Register<ICatalogRepository, CatalogRepository>();
            container.Register<IUserRepository, UserRepository>();

            container.Register<ICatalogService, CatalogService>();
            container.Register<IUserService, UserService>();
            container.Register<IAdminService, AdminService>();

            container.RegisterPerWebRequest<IUserStore<ApplicationUser, int>>(() => new CustomUserStore(container.GetInstance<ApplicationDbContext>()));
            container.RegisterPerWebRequest(() => new UserManager<ApplicationUser, int>(new CustomUserStore(container.GetInstance<ApplicationDbContext>())));
            container.Register(() => HttpContext.Current.GetOwinContext().Authentication);
        }

oliverslade avatar Mar 29 '16 23:03 oliverslade

Duplicate of https://stackoverflow.com/questions/36295878/registering-iuserstore-with-simple-injector-3-1-2

dotnetjunkie avatar Mar 30 '16 00:03 dotnetjunkie