I know there are several questions a bit like this one, but as I'm unable to locate any documentation and none of the other questions have any answers that help me, here goes:
I create a new ASP.NET application (VS2013), MVC is selected, I add API. I run "update-package" in the Package Console to get updated to latest versions (MVC 5.1.2, Web Api 5.1.2).
Then I add the new Ninject.MVC5 and Ninject.Web.WebApi packages.
I add these lines to the web.config file (Ninject wants version 5.0, I have 5.1):
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http.WebHost" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
</dependentAssembly>
In the App_start/NinjectWebCommon.cs file I add a single binding:
private static void RegisterServices(IKernel kernel) {
kernel.Bind<ILogger, NLogLogger>();
}
Then I add a single ApiController, with ILogger as the single parameter in the constructor, and I add ILogger as a parameter to the constructor of the HomeController.
That should be it for testing?
Am I missing some wiring for WebApi ? Or does that happen "behind the scenes"?
Anyway, acessing the api controller gives me this error:
An error occurred when trying to create a controller of type 'TestController'. Make sure that the controller has a parameterless public constructor.
And trying the HomeController gives me this:
Object reference not set to an instance of an object.
I've tried the same thing, without updating to latest packages, same problem. I've been using Ninject for years with MVC 3 (I've also tried the Ninject.MVC3 package), and am totally clueless as to why that doesn't work. With WebApi, I was kind of expecting some call to SetResolver, but..
Any help would be appriciated!
For using Ninject in an project with MVC
and WebApi
you have to install the following packages:
-Ninject
-Ninject.MVC5
-Ninject.Web
-Ninject.Web.Common
-Ninject.Web.Common.WebHost
-Ninject.Web.WebApi
-Ninject.Web.WebApi.WebHost
These packages add the following files to App_Start
folder:
NinjectWeb.cs:
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Smartiz.UI.NinjectWeb), "Start")]
namespace Smartiz.UI
{
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject.Web;
public static class NinjectWeb
{
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
}
}
}
NinjectWebCommon.cs:
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Extensions.Conventions;
using Ninject.Web.Common;
using Smartiz.ClientServices;
using Smartiz.UI;
using WebActivatorEx;
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(NinjectWebCommon), "Start")]
[assembly: ApplicationShutdownMethod(typeof(NinjectWebCommon), "Stop")]
namespace Smartiz.UI
{
public static class NinjectWebCommon
{
private static readonly Bootstrapper Bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
Bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
Bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
//kernel.Bind(q =>
// q.FromAssembliesMatching("Smartiz.ClientServices*")
// .SelectAllClasses()
// .InheritedFrom(typeof(BaseService))
// .BindDefaultInterface()
// .Configure(c => c.InSingletonScope()));
}
}
}
This is not a MVC/WebApi issue
your configuration is wrong:
kernel.Bind<ILogger, NLogLogger>();
should be
kernel.Bind<ILogger>().To<NLogLogger>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With