None of the constructors found can be invoked with

2019-08-11 03:59发布

问题:

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

回答1:

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();