在两个服务的构造函数之间检测到 C# 循环依赖项绑定中的注入错误



我有一个类,正如你用它的构造函数看到的:

public  class ReceptionService:IReceptionService
    {
        private readonly DataContext _ctx;
        private readonly IAutomotiveRepository _automotiveRepository;
        private readonly IReferredRepository _referredRepository;
        private readonly IReceptionRepository _receptionRepository;
        private readonly IReceptionService _receptionService;
        private readonly IAutomotiveService _automotiveService;
        private readonly IReferredService _referredService;
        public ReceptionService(DataContext ctx, IReferredRepository ReferredRepository, IAutomotiveRepository AutomotiveRepository,IReceptionRepository ReceptionRepository,IReceptionService ReceptionService,IReferredService ReferredService,IAutomotiveService AutomotiveService)
        {
            _ctx = ctx;
            _automotiveRepository = AutomotiveRepository;
            _referredRepository = ReferredRepository;
            _receptionRepository = ReceptionRepository;
            _referredService = ReferredService;
            _automotiveService = AutomotiveService;
            _receptionService = ReceptionService;
        }
}

在全局部分中,我定义了这一点:

 public class Global : NinjectHttpApplication
    {
        protected override IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Reception.Domain.Repository.IAutomotiveRepository>().To<Reception.Infrustructure.RepositoryImplement.AutomotiveRepository>().InRequestScope();
            kernel.Bind<Reception.Domain.Repository.IReceptionRepository>().To<Reception.Infrustructure.RepositoryImplement.ReceptionRepository>().InRequestScope();
            kernel.Bind<Reception.Domain.Repository.IReferredRepository>().To<Reception.Infrustructure.RepositoryImplement.ReferredRepository>().InRequestScope();
            kernel.Bind<Reception.Domain.Repository.ITestRepository>().To<Reception.Infrustructure.RepositoryImplement.TestRepository>().InRequestScope();
            kernel.Bind<Reception.Domain.Repository.IReceptionHistoryRepository>().To<Reception.Infrustructure.RepositoryImplement.ReceptionHistoryRepository>().InRequestScope();
            kernel.Bind<Reception.Domain.Repository.IGasReceptionHistoryRepository>().To<Reception.Infrustructure.RepositoryImplement.GasReceptionHistoryRepository>().InRequestScope();
            kernel.Bind<Reception.Domain.Repository.IGasReceptionRepository>().To<Reception.Infrustructure.RepositoryImplement.GasReceptionRepositoy>().InRequestScope();
            kernel.Bind<Reception.Domain.Repository.IGasTestRepository>().To<Reception.Infrustructure.RepositoryImplement.GasTestRepository>().InRequestScope();
            kernel.Bind<Reception.Domain.Repository.IHydrostaticReceptionHistoryRepository>().To<Reception.Infrustructure.RepositoryImplement.HydrostaticReceptionHistoryRepository>().InRequestScope();
            kernel.Bind<Reception.Domain.Repository.IHydrostaticReceptionRepository>().To<Reception.Infrustructure.RepositoryImplement.HydrostaticReceptionRepository>().InRequestScope();
            kernel.Bind<Reception.Domain.Repository.IHydrostaticTestRepository>().To<Reception.Infrustructure.RepositoryImplement.HydrostaticTestRepository>().InRequestScope();

            kernel.Bind<Reception.Domain.Service.IAutomotiveService>().To<Reception.Application.ServiceImplement.AutomotiveService>().InRequestScope();
            kernel.Bind<Reception.Domain.Service.IReceptionService>().To<Reception.Application.ServiceImplement.ReceptionService>().InRequestScope();
            kernel.Bind<Reception.Domain.Service.IReferredService>().To<Reception.Application.ServiceImplement.ReferredService>().InRequestScope();
            kernel.Bind<Reception.Domain.Service.ITestService>().To<Reception.Application.ServiceImplement.TestService>().InRequestScope();
            kernel.Bind<Reception.Domain.Service.IReceptionHistoryService>().To<Reception.Application.ServiceImplement.ReceptionHistoryService>().InRequestScope();
            kernel.Bind<Reception.Domain.Service.IGasReceptionHistoryService>().To<Reception.Application.ServiceImplement.GasReceptionHistoryService>().InRequestScope();
            kernel.Bind<Reception.Domain.Service.IGasReceptionService>().To<Reception.Application.ServiceImplement.GasReceptionService>().InRequestScope();
            kernel.Bind<Reception.Domain.Service.IGasTestService>().To<Reception.Application.ServiceImplement.GasTestService>().InRequestScope();
            kernel.Bind<Reception.Domain.Service.IHydrostaticReceptionHistoryService>().To<Reception.Application.ServiceImplement.HydrostaticReceptionHistoryService>().InRequestScope();
            kernel.Bind<Reception.Domain.Service.IHydrostaticReceptionService>().To<Reception.Application.ServiceImplement.HydrostaticReceptionService>().InRequestScope();
            kernel.Bind<Reception.Domain.Service.IHydrostaticTestService>().To<Reception.Application.ServiceImplement.HydrostaticTestService>().InRequestScope();

            kernel.Bind<DataContext>().To<DataContext>().InSingletonScope();
            return kernel;
        }
}

但是当我运行我的页面时,我收到此错误:

Error activating IReceptionService using binding from IReceptionService to ReceptionService
A cyclical dependency was detected between the constructors of two services.
Activation path:
 4) Injection of dependency IReceptionService into parameter ReceptionService of constructor of type ReceptionService
 3) Injection of dependency IReceptionService into parameter ReceptionService of constructor of type ReceptionService
 2) Injection of dependency ReceptionService into parameter instance of constructor of type NinjectIISHostingServiceHost{ReceptionService}
 1) Request for NinjectIISHostingServiceHost{ReceptionService}
Suggestions:
 1) Ensure that you have not declared a dependency for IReceptionService on any implementations of the service.
 2) Consider combining the services into a single one to remove the cycle.
 3) Use property injection instead of constructor injection, and implement IInitializable
    if you need initialization logic to be run after property values have been injected.

ReceptionService构造函数需要一个ReceptionService实例:

public ReceptionService(/.../, IReceptionService ReceptionService, /.../)
那么为了建立一个接待服务

,你首先需要建立一个接待服务?我怀疑是周期性依赖性混淆了框架。 除非有其他可用的构造函数,否则您可以以某种方式在依赖项注入器中配置,否则它将无限期地尝试通过尝试构建ReceptionService来构建ReceptionService

我想第一步是从概念上确定为什么该类需要自身的实例。 我怀疑它可能没有,尽管我不知道你在构建什么。

相关内容

  • 没有找到相关文章

最新更新