3
votes

I installed the NuGet package Ninject Integration for WebApi2 via the Package Manager Console.

According to the wiki on the project's GitHub pages, this should have created a class called NinjectWebCommon in the App_Start folder. But it didn't.

That same GitHub wiki page explains what you should see so that you can add it yourself. So I tried creating the NinjectWebCommon class myself. The problem here is that I can't find the namespace for OnePerRequestModule in the following snippet.

public static void Start()
{
    DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
    DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
    bootstrapper.Initialize(CreateKernel);
}

The alternative, and according to that GitHub wiki page there's no effective difference between the two, is to modify the global.asax. So I tried this method and added some bindings like so

private void RegisterServices(IKernel kernel)
{
    kernel.Bind<IEntityAccess>().To<EntityAccess>();
    kernel.Bind<IDictionaryRepository>().To<DictionaryRepository>();
}

and found that my project builds, but when a request is sent to the WebAPI project it can't be found (i.e., I receive a 404 response).

So there's obviously some other piece of necessary wiring up which isn't in my project.

It appears that despite changing my global.asax to derive from NinjectHttpApplication, the global.asax is no longer being called.

Can anyone tell me what I might be missing? I've uninstalled and reinstalled the NuGet package a few times but the NinjectWebCommon class never appears, nor does my global.asax file ever get modified.

I've also read Ninject's own documentation but frustratingly this is a fairly large tutorial covering the basics of IoC and how Ninject operates rather than telling you how to get started with using Ninject.

There's also this SO post asking how to get started with Ninject for WebAPI, so it looks like something's amiss with their NuGet package.

1

1 Answers

9
votes

And like that, I've just found the answer: there is an additional NuGet package which must be referenced:

Ninject.Web.WebApi.WebHost

Installing "Ninject integration for WebApi2" package is not sufficient.

This really should be more clearly explained.