Resolving “make sure that the controller has a parameter less public constructor” error in WebAPIs

Hi,

If you’re working with WebAPIs and in the your API Controller, you have a constructor that takes parameters which are being injected by Unity or any IoC Container, then there might be a case where you will get the below error when you try to hit an API from your controller.

Make sure that the controller has a parameterless public constructor. Below screen shot is from Fiddler which shows the exception response in more detail. Click on the image to see it more clearly.

fiddler-response

The reason why it is expecting a default (parameterless) constructor is because the IoC Container was unable to resolve all or some of the parameters of the constructor. Say for example, your constructor is as shown below.

constructor

So, in this case, if the implementations of the above interfaces are in a different assembly than that of the interfaces itself and you have missed adding reference of the assembly which actually has the implementation, then the IoC Container cannot resolve the dependency and thus it will fail to call the above parametered constructor.

So when you make a call to one of the APIs, IoC Container fails to inject the dependencies in the above controller and thus expects it to have a default constructor which is absent in our case. Thus the above exception! So to fix this, make sure you have added all required references (All those which have implementations of the required interfaces) in your project.

Hope this helps!!

Leave a Reply

Your email address will not be published. Required fields are marked *