ASP.NET Core DI无法解析类型服务

时间:2020-06-26 13:30:04

标签: asp.net asp.net-mvc asp.net-core dependency-injection saaskit

我正在使用ASP.NET Core创建一个应用程序。在其中,我创建了一个自定义line_row = -1 file = open(file_path, 'r') for number_of_lines in file: line_row = line_row + 1 if '1234' in number_of_lines: lines = file.readlines() line = lines[line_row] print(lines) lines[line_row] = 'hello' file = open(file_path, "w") file.writelines(lines) file.close() 以及一些服务。另外,我正在使用SAAS Kit进行多租户。但是我遇到一个错误,提示无法解决服务。

以前,一切正常,但是在进行一些更改后突然出现此错误。这是我的代码:

用于多租户的AppTenantResolver

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

课程服务:

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

仅当我在ApplicationUserManager中注入CourseService时才发生错误,为什么?只要从StudentService中删除CourseService,代码就可以正常工作。

0 个答案:

没有答案