配置无效。 Service类型直接或间接依赖于自身

The configuration is invalid. The type Service is directly or indirectly depending on itself

错误:

The configuration is invalid. Creating the instance for type IUserService failed. The configuration is invalid. The type PatientService is directly or indirectly depending on itself. The cyclic graph contains the following types: PatientService -> ConfigService -> PatientService.

代码:

var container = new Container();
container.Register<IUserService, UserService>();
container.Register<IPatientService, PatientService>();
container.Register<IConfigService, ConfigService>();

container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.Verify();

患者服务:

public class PatientService : IPatientService
    {
        private readonly IPatientRepository _patientRepository;
        private readonly ConfigService _configService;
        private readonly UserService _userService;

        public PatientService(
            PatientRepository patientRepository, 
            ConfigService configService, 
            UserService userService)
        {
            _patientRepository = patientRepository;
            _configService = configService;
            _userService = userService;
        }
}

配置服务:

public class ConfigService : IConfigService
    {
        private readonly PatientService _patientService;
        private readonly IPatientRepository _patientRepository;

        public ConfigService(
            PatientService patientService, PatientRepository patientRepository)
        {
            _patientService = patientService;
            _patientRepository = patientRepository;
        }
}

I have to use PatientService object in ConfigService and ConfigService object in PatientService. Is there a way to tackle this issue?

The configuration is invalid. Creating the instance for type IUserService failed. The configuration is invalid. The type PatientService is directly or indirectly depending on itself. The cyclic graph contains the following types: PatientService -> ConfigService -> PatientService.

PatientService 使用了 ConfigService 的引用。 ConfigService 使用 PatientService

的引用

您需要删除源代码中的某处 ConfigService。因为您要在此处添加循环依赖,这不是一个好的做法,所以您需要检查您的设计。