Search code examples
c#asp.net-coredependency-injectionautofac

.NET Core Web API An exception was thrown while activating


I'm getting the following exception when sending a post request to my web api:

Autofac.Core.DependencyResolutionException: An exception was thrown while activating WAM4.WebAPI.Core.UseCases.RegisterUserUseCase -> WAM4.WebAPI.Infrastructure.Data.Repositories.UserRepository -> Microsoft.AspNetCore.Identity.UserManager1[[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.UserManager1[WAM4.WebAPI.Infrastructure.Identity.AppUser]' can be invoked with the available services and parameters: Cannot resolve parameter 'Microsoft.AspNetCore.Identity.IUserStore1[WAM4.WebAPI.Infrastructure.Identity.AppUser] store' of constructor 'Void .ctor(Microsoft.AspNetCore.Identity.IUserStore1[WAM4.WebAPI.Infrastructure.Identity.AppUser], Microsoft.Extensions.Options.IOptions1[Microsoft.AspNetCore.Identity.IdentityOptions], Microsoft.AspNetCore.Identity.IPasswordHasher1[WAM4.WebAPI.Infrastructure.Identity.AppUser], System.Collections.Generic.IEnumerable1[Microsoft.AspNetCore.Identity.IUserValidator1[WAM4.WebAPI.Infrastructure.Identity.AppUser]], System.Collections.Generic.IEnumerable1[Microsoft.AspNetCore.Identity.IPasswordValidator1[WAM4.WebAPI.Infrastructure.Identity.AppUser]], Microsoft.AspNetCore.Identity.ILookupNormalizer, Microsoft.AspNetCore.Identity.IdentityErrorDescriber, System.IServiceProvider, Microsoft.Extensions.Logging.ILogger1[Microsoft.AspNetCore.Identity.UserManager1[WAM4.WebAPI.Infrastructure.Identity.AppUser]])'.

These are my 2 modules that get "build()" into the container and the AppUser class:

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

I've checked to see that UserRepository is properly registered in Load, and that AppUser class is properly compiled. Any guidance on what area I should be looking into to resolve this exception, would be greatly appreciated.


Solution

  • Alright, I figured out what I was doing wrong.

    I omitted the JWTToken code from the template Startup.cs, because I hadn't gotten to that part yet. Big Mistake, spent the whole day thinking it was a different issue...Made my startup.cs the same as the template I was following (including all the JWT and Authentication code)...and BAM, easy fix. only took a whole 8 hours to figure out what I was doing wrong.

    Thank you llya for trying to help, appreciate it very much.