Reputation: 1310
Error:
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.
Code:
var container = new Container();
container.Register<IUserService, UserService>();
container.Register<IPatientService, PatientService>();
container.Register<IConfigService, ConfigService>();
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.Verify();
PatientService:
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;
}
}
ConfigService:
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?
Upvotes: 1
Views: 1747
Reputation: 23220
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
uses a reference of ConfigService
. ConfigService
uses a reference of PatientService
You need to remove ConfigService
somewhere in your source code. Because you're adding here a circular dependency which is not a good practice so you need to review your design.
Upvotes: 3