Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterless constructor error with Ninject bindings in .NET Web Api 2.1

Tags:

Working currently on writing an API site (.NET Web Api 2.1)

For our prior API sites we had used the Ninject.MVC3 package and wired up a dependency resolver and scope manually and plugged in our logic into NinjectWebCommon as per the recommendation.

This was causing heartburn int our new project with the parameterless constructor error. In the past, this was a dead give away that we were not wiring up the dependency resolver in Web Api properly. Only this time, we were. It was there.

var resolver = new NinjectDependencyResolver(kernel);
GlobalConfiguration.Configuration.DependencyResolver = resolver;

So I was a tad lost. In the last week, I saw that Ninject was updated, the WebAPI nuget package was revived so I decided to try and implement.

I installed the NuGet package Ninject.Web.WebApi (version 3.2). This didn't seem to include a NinectWebCommon.cs file to apply bindings. It only added the necessary assemblies. Doing some digging I stumbled across this implementation.

https://gist.github.com/odytrice/5842010

(I was unclear if the Nuget package was doing this for me and this is all redundant code. If so, great, I just need an understanding of how to add our bindings with the nuget package now.)

I implemented this line for line except where loading the modules, I loaded our three individual modules as a test.

public static INinjectModule[] Modules
{
    //get { return new INinjectModule[] { new MainModule() }; }
    get { return new INinjectModule[] { new GlobalModule(), new InventoryModule(), new StoreModule() }; }
    }

I've verified that the dependency resolver is being set with this new code.

_resolver = new NinjectHttpResolver(modules);
GlobalConfiguration.Configuration.DependencyResolver = _resolver;

But still, the problem persists.

ExceptionMessage:

An error occurred when trying to create a controller of type 'StoreSearchController'.
Make sure that the controller has a parameterless public constructor.

StackTrace:

at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) 
at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) 
at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) 
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()

Inner Exception Exception Type: System.ArgumentException

StackTrace:

at System.Linq.Expressions.Expression.New(Type type) 
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType) 
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) 
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)

I'm at a loss. I've seen lots of posts around having to set a MVC dependency resolver as well, but didn't think that was related as in this case we have no MVC controllers outside of the home controller which does not use Ninject.

One other tidbits that may or may not matter. We are using attribute routing.

Any help is appreciated.

like image 880
Khepri Avatar asked Mar 31 '14 17:03

Khepri


2 Answers

You have to install one of the hosting packages:

  • Ninject.Web.WebApi.WebHost
  • Ninject.Web.WebApi.SelfHost
  • Ninject.Web.WebApi.OwinHost
like image 71
Remo Gloor Avatar answered Sep 22 '22 12:09

Remo Gloor


Also, double check that your constructor is Public. This is easy to overlook.

like image 2
Aaron Avatar answered Sep 23 '22 12:09

Aaron