Make sure that the controller has a parameterless public constructor in Unity

后端 未结 2 1442
梦如初夏
梦如初夏 2021-01-12 06:27

I got this problem with the Controller:
An error occurred when trying to create a controller of type \'*.WebMvc.Controllers.HomeController\'. Make sure that t

相关标签:
2条回答
  • 2021-01-12 07:00

    This can also be due to an exception in the parameter-injected constructor of the outer type that is being resolved. The dependencies of that type's constructor might be getting resolved successfully, but if there is an exception in the outer constructor, Unity will report it as "Type Test.Controllers.MyControllerWithInjectedDependencies does not have a default constructor".

    0 讨论(0)
  • 2021-01-12 07:14

    Found where is the issue. Maybe some one will face the same. The problem is that Unity could not resolve the IAccountingUow, because of hierarchical dependancy on interfaces.

    AccountingUow class has two contuctors

        public AccountingUow( IRepositoryProvider repositoryProvider)
        {
            Init(repositoryProvider);
        }
        public AccountingUow()
        {
            Init( new RepositoryProvider(new RepositoryFactories()) );
        }
    

    Dependency Resolver is not smart enought to take the default parametless contructor. It tries to take interface dependant contructor and fails to resolve it, cause there are no rules for resolving it.

    I commented out interface dependant constructor and everything worked fine.

    I will post later resolver for the first contructor, maybe someone will use it.

    0 讨论(0)
提交回复
热议问题