None 个找到的构造函数可以使用可用的服务和参数 Autofac 调用

None of the constructors found can be invoked with the available services and parameters Autofac

我已阅读并按照示例进行编码:http://timschreiber.com/2015/01/14/persistence-ignorant-asp-net-identity-with-patterns-part-1/ 但是该示例使用 Unity 进行 DI,但我使用 Autofac 作为 ID,当我尝试 运行 我的项目时出现以下错误:

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])'.

我的代码:

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

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

}

谁能帮帮我!

错误消息告诉您 Autofac 无法创建 HomeController 的实例,因为它找不到其 Microsoft.AspNet.Identity.UserManager<IdentityStore.IdentityUser,System.Guid> userManager 参数。

好像没有注册过这个类型。要解决此错误,您必须注册一个 UserManager<IdentityUser, Guid>。您可以在 IdentityModule class 中执行此操作:

builder.RegisterType<UserManager<IdentityUser, Guid>>()
       .As<UserManager<IdentityUser, Guid>>()
       .InstancePerRequest();