Web Api RC – DependencyResolver GOTCHA

Recently when working with the Web Api RC I ran into a problem, that later occurred to me to be  caused by a known issue. So I thought that it might be of help to share the cause and solution.

The problem occurs when  a controller with a dependency works, and then suddenly stops working with an exception like;

{"ExceptionType":"System.ArgumentException","Message":"Type 'YourProject.Controllers.DemoController' does not have a default constructor"..."

This is due to an issue in the fastcache. If you have a controller with no dependencies that is not registered so it could be resolved by the DependecyResolver and one registered that has dependencies. As soon as the non  registered controller is created the cache is in a state that will return null when the DependecyResolver later tries to resolve the controller with a dependency.

Probably the IoC-container that you are using have the equivalent of;

builder.RegisterControllers(Assembly.GetExecutingAssembly());

for your ApiControllers. Then you would never see this issue.
But if you manually register your controllers for some reason, this might get you puzzled for a while.

The Issue could be found over @ codeplex.

Hope this helps If you got this problem.

Enjoy!

Leave a Reply