3

I have read and coding follow example: http://timschreiber.com/2015/01/14/persistence-ignorant-asp-net-identity-with-patterns-part-1/ But that example used Unity for DI, but i'm used Autofac for ID, when i try run my project i have the following error:

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'App.Front.Controllers.HomeController' can be invoked with the available services and parameters: Cannot resolve parameter 'Microsoft.AspNet.Identity.UserManager2[IdentityStore.IdentityUser,System.Guid] userManager' of constructor 'Void .ctor(Microsoft.AspNet.Identity.UserManager2[IdentityStore.IdentityUser,System.Guid])'.

My code:

public class IdentityModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        //var dbContextParameter = new ResolvedParameter((pi, ctx) => pi.ParameterType == typeof(DbContext),
        //                                               (pi, ctx) => ctx.Resolve<AppContext>());
        builder.RegisterType<UserStore>().As<IUserStore<IdentityUser, Guid>>().InstancePerLifetimeScope();
        builder.RegisterType<RoleStore>().InstancePerLifetimeScope();
    }
}

and

private static void SetAutofacContainer()
{
    var builder = new ContainerBuilder();
    var mainModules = BuildManager.GetReferencedAssemblies().Cast<Assembly>().Where(p => p.ManifestModule.Name.StartsWith("App.")).ToArray();
    builder.RegisterControllers(mainModules);
    builder.RegisterAssemblyModules(mainModules);

    builder.RegisterFilterProvider();
    builder.RegisterSource(new ViewRegistrationSource());

    builder.RegisterModule(new EFModule());
    builder.RegisterModule(new RepositoryModule());
    builder.RegisterModule(new IdentityModule());
    builder.RegisterModule(new ServiceModule());
    var container = builder.Build();
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

}

Any one help me please!.

  • I could really use all of the modules you used to set up Identity via AutoFac. For some reason, my implementation is experiencing the same issue, but I know almost nothing about AutoFac; especially how to set it up. – René Kåbis Dec 03 '16 at 02:03

1 Answers1

2

The error message tells you that Autofac can't create an instance of HomeController because it can't find its Microsoft.AspNet.Identity.UserManager<IdentityStore.IdentityUser,System.Guid> userManager parameter.

It seems that this type has not been registered. To solve this error you have to register a UserManager<IdentityUser, Guid>. You can do this in your IdentityModule class :

builder.RegisterType<UserManager<IdentityUser, Guid>>()
       .As<UserManager<IdentityUser, Guid>>()
       .InstancePerRequest(); 
Cyril Durand
  • 15,834
  • 5
  • 54
  • 62
  • Thanks you for your help, my code working now, but i wonder in that example, his code custom UserStore class, if i using your code, my project working now, but when i create and login user my code have an error: [InvalidOperationException: UserId not found.] My code Signin var identity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); – Trường Sơn Mar 28 '16 at 18:36
  • @TrườngSơn I'm not sure to understand. Could you edit your question and explain your issue ? – Cyril Durand Mar 29 '16 at 07:08
  • I have run into the same problem, but it is complicated by the fact that I am using the MVC HTML5 template, which already includes AutoFac and which doesn’t use OP's pattern. When I try to include the solution from @CyrilDurand it doesn't work. Totally new to DI, completely lost. – René Kåbis Dec 03 '16 at 00:55
  • Just register type like me: builder.RegisterType>() .As>() .InstancePerRequest(); – Trường Sơn Dec 03 '16 at 08:46