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

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.


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".


Comments

  1. Gideon

    • 2018/2/21

    Basically, what happened is that you didn't register your controllers explicitly in your container. Unity tries to resolve unregistered concrete 

  2. Bridger

    • 2018/4/9

    I am using the Unity.WebApi NuGet package (Unity 4.0.1 and Unity.WebApi 5.2.3) in an ASP.NET WebApi solution. The issue I am facing is that when attempting to run the code, I get the error: Make sure that the controller has a parameterless public constructor. I've searched similar threads here, but I couldn't find any thread that matched my issue.

  3. Davis

    • 2015/1/30

    Take both the unity container and the http configuration out of using statements. They are both getting disposed before the first web request is 

  4. Pablo

    • 2019/2/3

    4 Answers4. That depends on how you're handling DI (dependency injection). By default, controllers need paramaterless constructor, and you have to use something like Unity or Ninject to handle DI. I see you're inhereiting from ApiController, so if you're using Ninject make sure you download Ninject for Web API.

  5. Kobe

    • 2017/2/4

    Please recheck all register interface services in Unity container. Maybe your code looks like as below, you will get this error : container.

  6. Dilan

    • 2018/10/21

    Since Unity returns null, Web API will try to create the controller itself, but since it doesn't have a default constructor it will throw the "Make sure that the controller has a parameterless public constructor" exception. This exception message is misleading and doesn't explain the real cause.

  7. Aayan

    • 2016/4/14

    Make sure that the controller has a parameterless public constructor." for this i have checked unity config. boostrapper class. but no luck 

  8. Roux

    • 2017/10/7

    Make sure that the controller has a parameterless public constructor. Note: I used the Unity container to init all service for the application. Thank you for any suggestions.

  9. Giordano

    • 2020/4/10

    Unity tries to resolve unregistered concrete types for you, but because it can't resolve it (caused by an error in your configuration), it return null. It is 

  10. Jaxton

    • 2018/8/3

    Since Unity returns null, Web API will try to create the controller itself, but since it doesn't have a default constructor it will throw the "Make sure that the controller has a parameterless public constructor" exception. This exception message is misleading and doesn't explain the real cause.

  11. Darren

    • 2021/1/31

    Make sure that the controller has a parameterless public constructor. The error message is not hugely helpful. If we look up LegacyTreeController, it does have 

  12. Jose

    • 2020/12/17

    I think your using UNITY, registering of controller is not proper in unity config. It actually needs controller instance created but its not happening here. Post necessary code to get clear idea i.e. unity config, controller, web api bootstarp classess

  13. Kohen

    • 2017/5/18

    Since Unity returns null, Web API will try to create the controller itself, but since it doesn't have a default constructor it will throw the "Make sure that 

  14. Melvin

    • 2015/10/6

    No parameterless constructor defined for this object. Description: An unhandled exception occurred. Make sure that the controller has a parameterless public

  15. Jose

    • 2017/6/7

    Make sure that the controller has a parameterless public constructor. Solution. We can configure KissLog to capture and log Unity exceptions, which contains 

  16. Braydon

    • 2017/7/4

    Make sure that the controller has a parameterless public constructor.Details are: at System.Web.Mvc.DefaultControllerFactory.

Comments are closed.

Recent Posts