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!!

Advertisements

About Amogh Natu

Technology enthusiast, Associate Consultant @ Microsoft, music lover, love my guitar, Microsoft.NET, ASP.NET, C#.NET Professional.
This entry was posted in .NET and tagged , , , , , , , , . Bookmark the permalink.

Your comments or opinions.....

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s