Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Constructor taking a long time to initialize. #4464


User avatar
0
sparkyjr created

Hi,

When I make a web request its taking too long for the constructor (MVC Controller as well as AppService) to get initialized. The thing is that the constructor has some services injected which in turn has some other services injected.

The example is as follows:

public class Example1AppService : AcmeAppServiceBase, IExample1AppService
    {
        private readonly IRepository<entity1> _entity1Repository;
        private readonly IExample2AppService _example2AppService;
        private readonly IExample3AppService _example3AppService;
        private readonly IExample4AppService _example4AppService; 

        public Example1AppService(
            IRepository<entity1> entity1Repository,
            IExample2AppService example2AppService,
            IExample3AppService example3AppService,
             IExample4AppService  example4AppService
            )
        {
            _entity1Repository =entity1Repository;
            _example1AppService = example1AppService; 
            _example2AppService = example2AppService;
            _example4AppService = example4AppService;
        }

and Example2AppService has a lot of Repositories as well as services injected in its constructor. And time taken to hit constructor of Example1AppService is quite long and measurable.

I have worked with dependency injection in different project before and have never faced issue with constructor intialization. The IOC container that we used was Autofac.

Thanks in advance


2 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Your code sample shows IExampleAppService constructor-injecting itself. I hope you're not actually doing that.

    The thing is that the constuctor has some services injected which in turn has some other services injected.

    You should use a domain service/manager instead. See detailed answers: Should I be calling an AppService from another AppService?

    Related topic: #4335@c2599492-9660-4818-9c17-cadb7209c8ce


    Tip: Wrap your code in the following for formatting and readability:

    [code]
    

    [/code:1yng7j8p] A shortcut is to highlight your code and click on the </> button in the formatting toolbar.

  • User Avatar
    0
    sparkyjr created

    Hi Aaron,

    My mistake! Apologies, it was a typo from my side! I have corrected my question.

    I will take a look at links you have shared, and get back to you if it's needed.

    Thanks, SparkyJr