Haseeb Khan
Haseeb Khan

Reputation: 1050

ASP.NET Core DI unable to resolve service for type

I am creating an application using ASP.NET Core. In which I have created a custom ApplicationUserManager along with a couple of services. Also, I am using SAAS Kit for multitenancy. But I am stuck with an error which says unable to resolve service.

Previously It was all working fine but all of a sudden after some change this error occurred. Here is my code:

AppTenantResolver For Multi-Tenancy

public class AppTenantResolver : MemoryCacheTenantResolver<TenantEditModel>
{
    private readonly ApplicationUserManager _userManager;
    private readonly TenantService _tenantService;
    private readonly IMapper _mapper;

    public AppTenantResolver(
        IMapper mapper,
        TenantService tenantService,
        ApplicationUserManager userManager,
        IMemoryCache cache,
        ILoggerFactory loggerFactory
        ) : base(cache, loggerFactory)
    {
        _userManager = userManager;
        _mapper = mapper;
        _tenantService = tenantService;
    }
    
    protected override string GetContextIdentifier(
        HttpContext context)
    {
        return context.Request.Host.Value.ToLower();
    }

    protected override IEnumerable<string> GetTenantIdentifiers(
        TenantContext<TenantEditModel> context)
    {
        return new[] { "localhost:44321" };
    }

    protected override async Task<TenantContext<TenantEditModel>> ResolveAsync(
        HttpContext context)
    {
        TenantContext<TenantEditModel> tenantContext = null;
        var host = context.Request.Host.Value.ToLower();

        if (context.User.Identity.IsAuthenticated && context.User.IsInRole("Admin"))
        {
            var user = await _userManager.GetUserAsync(context.User);
            var tenant = await _tenantService.GetByIdAsync(user.TenantId);
            if (tenant != null)
            {
                tenantContext = new TenantContext<TenantEditModel>(
                    _mapper.Map<TenantEditModel>(tenant));
                return tenantContext;
            }
        }
        return new TenantContext<TenantEditModel>(new TenantEditModel());
    }
}

StudentService:

public class StudentService
{
    private readonly IMapper _mapper;
    private readonly CourseService _courseService;
    private readonly IStudentRepository _studentRepository;
    private readonly IUnitOfWork _unitOfWork;
    
    public StudentService(
        IMapper mapper,
        IStudentRepository studentRepository,
        IUnitOfWork unitOfWork,
        CourseService courseService)
    {
        _studentRepository = studentRepository;
        _mapper = mapper;
        _unitOfWork = unitOfWork;
        _courseService = courseService;
    }

    public async Task CreateAsync(MongoIdentityUser user)
    {
        try
        {
            var student = _mapper.Map<Student>(user);
            _unitOfWork.Students.Add(student);
            await _unitOfWork.CommitAsync();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

CourseService:

public class CourseService
{
    private readonly IFileUploadProcessor _fileUploadProcessor;
    private readonly IAppConfiguration _appConfiguration;
    private readonly IUnitOfWork _unitOfWork;
    private readonly ITenantRepository _tenantRepository;
    private readonly TenantEditModel _tenant;
    private readonly IMapper _mapper;
    
    public CourseService(
        IUnitOfWork unitOfWork,
        ITenantRepository tenantRepository,
        TenantEditModel tenant,
        IMapper mapper,
        IAppConfiguration appConfiguration,
        IFileUploadProcessor fileUploadProcessor)
    {
        _unitOfWork = unitOfWork;
        _tenantRepository = tenantRepository;
        _tenant = tenant;
        _mapper = mapper;
        _fileUploadProcessor = fileUploadProcessor;
        _appConfiguration = appConfiguration;
    }

    public async Task<List<CourseListModel>> GetAllAsync()
    {
        var tenant = await _unitOfWork.Tenants.GetByIdAsync(_tenant.Id);
        return _mapper.Map<List<CourseListModel>>(tenant.Courses);
    }
}

The error only occurred when I inject CourseService in StudentService why? As soon as I remove CourseService from StudentService code works fine.

Upvotes: 0

Views: 277

Answers (0)

Related Questions