2

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<>), new[] { typeof(ApplicationUser) });

        //container.Register<IUserStore<>, store>();

        //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.

PYROv1
  • 143
  • 1
  • 10
  • Please check this link, it has solution very well explained. http://stackoverflow.com/questions/30602687/simple-injector-identity-usermanagerappuser-int32-registration-error – Husein Mar 29 '16 at 22:35
  • I have tried to use the examples from the link posted already and they seem to either apply to a different version of Simple Injector or they dont work with my solution. – PYROv1 Mar 29 '16 at 22:40

1 Answers1

3

I answered my own question. I used various other stack overflow questions and the answer was to edit my SimpleInjectorInitializer as follows:

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.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);
    }
}

Note: container.Verify() will prevent this solution from working but it does work. I need to do further research to find a better way of doing this whilst using container.Verify().

Steven
  • 166,672
  • 24
  • 332
  • 435
PYROv1
  • 143
  • 1
  • 10
  • 1
    Don't forget to register `ApplicationDbContext` explicitly in the container and don't forget to call Verify(). – Steven Mar 29 '16 at 23:59
  • 1
    "container.Verify() will prevent this solution from working but it does work". Calling `Verify` will fail, because the solution is flawed. Verify fails, because `ApplicationDbContext` is a transient, which isn't disposed. Try registering `ApplicationDbContext` as scoped (web request) intance instead. – Steven Mar 31 '16 at 13:41