Home > Mobile >  How can I prevent my dependency from loading multiple times at the start of a .NET Core Application?
How can I prevent my dependency from loading multiple times at the start of a .NET Core Application?

Time:12-10

I have a .Net Core application where we are using dependency injection. All of our providers, as we call them, are registered in the Startup.cs file and are set up as services.AddTransient providers. I have a controller that injects one of the providers as a dependency, and that provider in turn injects a few other dependencies.

The problem I am having is that one of the other providers that is injected into the first one, is being called 8 times. I have placed a break point on a few lines in the constructor method for the second dependency, and I can see it's being called 8 times. I went through the dependency chain and could not find a reason for this. I also stepped through the start of the application and noticed there were a number of dependencies that were created, that had nothing to do with the provider I was starting with. Am I using dependency injection incorrectly? I just can't find an explanation as to why it would load a dependency 8 times.

This is part of my startup class, if it helps.

            services.AddTransient<IResourceProvider, ResourceProvider>();
            services.AddTransient<IStaticListProvider, StaticListProvider>();
            services.AddTransient<IPlanActivityLineItemProvider, PlanActivityLineItemProvider>();
            services.AddTransient<IPlanRevisionProvider, PlanRevisionProvider>();
            services.AddTransient<IProjectImportExportProvider, ProjectImportExportProvider>();
            services.AddTransient<IExpandoParserProvider, ExpandoParseProvider>();
            services.AddTransient<IProjectPlanProvider, ProjectPLanProvider>();
            services.AddTransient<IActualsImportProvider, ActualsImportProvider>();
            services.AddTransient<IReportProvider, ReportProvider>();
            services.AddTransient<IProjectWorkFlowProvider, ProjectWorkflowProvider>();

EDIT: Sorry, I didn't provide enough code to help with this. Our controller takes in a dependency, we call that class a Provider. Most of our providers also inject one or more dependencies. The code below is the start of the provider class, along with the constructor method. Hopefully this explains the Provider issue. I placed a break point on one of the lines of the constructor, and as the application was starting, it stopped at this break point 8 times, which leads me to believe that it's instantiating the provider 8 times. And I can't find a reason for this. I followed the dependency chain and it's not like it's being called again in another provider. I don't see a logical explanation for it. When an application starts, is every single service instantiated?

        private readonly IURCSRepo _repo;
        private readonly IPlanRevisionProvider _planRevisionProvider;
        private readonly IExcelFileAndSheetValidationProvider _excelImportValidationProvider;
        private readonly IExcelExtensionsProvider _excelExtensionsProvider;
        private readonly IEmailProvider _emailProvider;
        private readonly string _rpmActuals = "Actuals";
        private TableParser<ActualHoursLineItemImportModel> _rpmLineItemTableParser;
        private List<RoleDescriptionModel> roleModels = new List<RoleDescriptionModel>();
        private List<PlanResourceModel> resources = new List<PlanResourceModel>();
        private List<ProjectPlanHeaderModel> projectPlanHeaders = new List<ProjectPlanHeaderModel>();
        private List<ChargingStrategyModel> chargingStrategies = new List<ChargingStrategyModel>();
        private List<HoursTypeModel> hoursTypes = new List<HoursTypeModel>();
        private readonly UserManager<ApplicationUser> _userManager;

        public ActualsImportProvider(IURCSRepo uRCSRepo, IPlanRevisionProvider planRevisionProvider, IExcelFileAndSheetValidationProvider excelFileAndSheetValidationProvider, IExcelExtensionsProvider excelExtensionsProvider, IEmailProvider emailProvider, UserManager<ApplicationUser> userManager)
        {
            _repo = uRCSRepo;
            _planRevisionProvider = planRevisionProvider;
            _excelImportValidationProvider = excelFileAndSheetValidationProvider;
            _excelExtensionsProvider = excelExtensionsProvider;
            _rpmLineItemTableParser = new TableParser<ActualHoursLineItemImportModel>(_excelExtensionsProvider);
            _emailProvider = emailProvider;
            roleModels = _repo.GetAllRoleDescriptions(true).MakeRoleDescriptions();
            resources = _repo.GetAllActiveResources().MakePlanResources();
            projectPlanHeaders = _repo.GetAllProjectPlanHeaders().MakeProjectPlanHeaderList();
            chargingStrategies = _repo.GetAllChargingStrategies().MakeChargingStrategies();
            hoursTypes = _repo.GetAllHoursTypes(true).MakeHoursTypes();
            _userManager = userManager;
        }

CodePudding user response:

Transient objects are always different; a new instance is provided to every controller and every service (your case I presume).

Scoped objects are the same within a request, but different across different requests.

Singleton objects are the same for every object and every request.

CodePudding user response:

You could register your services as Singletons so they only get instantiated once:

services.AddSingleton<ISomeService, SomeService>();
  • Related