.NET Core Web API激活时引发了异常

时间:2019-05-15 17:47:18

标签: c# asp.net-core dependency-injection autofac

向我的Web api发送发布请求时出现以下异常:

  

Autofac.Core.DependencyResolutionException:引发了异常   在激活WAM4.WebAPI.Core.UseCases.RegisterUserUseCase->时   WAM4.WebAPI.Infrastructure.Data.Repositories.UserRepository->   Microsoft.AspNetCore.Identity.UserManager 1[[WAM4.WebAPI.Infrastructure.Identity.AppUser, WAM4.WebAPI.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. ---> Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Microsoft.AspNetCore.Identity.UserManager 1 [WAM4.WebAPI.Infrastructure.Identity.AppUser]'   可以使用可用的服务和参数来调用:       无法解析参数'Microsoft.AspNetCore.Identity.IUserStore 1[WAM4.WebAPI.Infrastructure.Identity.AppUser] store' of constructor 'Void .ctor(Microsoft.AspNetCore.Identity.IUserStore 1 [WAM4.WebAPI.Infrastructure.Identity.AppUser],   Microsoft.Extensions.Options.IOptions 1[Microsoft.AspNetCore.Identity.IdentityOptions], Microsoft.AspNetCore.Identity.IPasswordHasher 1 [WAM4.WebAPI.Infrastructure.Identity.AppUser],   System.Collections.Generic.IEnumerable 1[Microsoft.AspNetCore.Identity.IUserValidator 1 [WAM4.WebAPI.Infrastructure.Identity.AppUser]],   System.Collections.Generic.IEnumerable 1[Microsoft.AspNetCore.Identity.IPasswordValidator 1 [WAM4.WebAPI.Infrastructure.Identity.AppUser]],   Microsoft.AspNetCore.Identity.ILookupNormalizer,   Microsoft.AspNetCore.Identity.IdentityErrorDescriber,   System.IServiceProvider,   Microsoft.Extensions.Logging.ILogger 1[Microsoft.AspNetCore.Identity.UserManager 1 [WAM4.WebAPI.Infrastructure.Identity.AppUser]])')。

这是我的2个将“ build()”放入容器和AppUser类的模块:

    public class InfrastructureModule : Module
        {
            protected override void Load(ContainerBuilder builder)
            {
                builder.RegisterType<UserRepository>().As<IUserRepository>().InstancePerLifetimeScope();
                builder.RegisterType<JwtFactory>().As<IJwtFactory>().SingleInstance().FindConstructorsWith(new InternalConstructorFinder());
                builder.RegisterType<JwtTokenHandler>().As<IJwtTokenHandler>().SingleInstance().FindConstructorsWith(new InternalConstructorFinder());
                builder.RegisterType<TokenFactory>().As<ITokenFactory>().SingleInstance();
                builder.RegisterType<JwtTokenValidator>().As<IJwtTokenValidator>().SingleInstance().FindConstructorsWith(new InternalConstructorFinder());
                builder.RegisterType<Logger>().As<ILogger>().SingleInstance();
            }
        }


public class CoreModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<RegisterUserUseCase>().As<IRegisterUserUseCase>().InstancePerLifetimeScope();
            builder.RegisterType<LoginUseCase>().As<ILoginUseCase>().InstancePerLifetimeScope();
            builder.RegisterType<ExchangeRefreshTokenUseCase>().As<IExchangeRefreshTokenUseCase>().InstancePerLifetimeScope();
        }
    }

using Microsoft.AspNetCore.Identity;
//using System;
//using System.ComponentModel.DataAnnotations;

namespace WAM4.WebAPI.Infrastructure.Identity
{
    public class AppUser : IdentityUser
    {
        //Add additional profile data for application users by adding properties to this class
        //[Required]
        //[MaxLength(100)]
        //public string FirstName { get; set; }

        //[Required]
        //[MaxLength(100)]
        //public string LastName { get; set; }

        ////[Required]
        ////public byte SecurityLevel { get; set; }

        //[Required]
        //public DateTime CreateDate { get; set; }

        //public int Pending { get; set; }

        //public string Avatar { get; set; }
    }
}

我检查了UserRepository是否已在Load中正确注册,并且AppUser类已正确编译。非常感谢您提供有关我应该在哪个领域解决该异常的任何指导。

2 个答案:

答案 0 :(得分:1)

如果没有确切的注册信息,这会有点困难,但是看来您在容器中缺少IUserStore的注册信息。您可以通过以下方式验证其是否已注册:完成所有注册后是否解决?

由于它是通用接口IUserStore<TUser>,是否可能未向WAM4.WebAPI.Infrastructure.Identity.AppUser注册正确的通用版本

答案 1 :(得分:0)

好的,我发现我做错了什么。

我从模板Startup.cs中省略了JWTToken代码,因为我还没有涉及到那部分。一大错误,整天都在考虑这是一个不同的问题...使我的startup.cs与我遵循的模板相同(包括所有JWT和Authentication代码)...以及BAM,易于修复。只花了整整8个小时才弄清楚我做错了什么。

非常感谢llya的帮助,非常感谢。