UPDATE - Please look at my answer for a link and explanation of the solution to this problem
Before we start, I know this is a very common question and I've been using Ninject for many moons without issues, but now it's come up and I can't figure out a fix. Also, no, none of the results on Google and SO so far have helped me.
So, consider the following bit of code running on a very, very, very simple prototype ASP.NET MVC 4 project from Visual Studio 2012 on Windows Server 2008 R2:
public class DefaultController : Controller {
private IGroupPrincipalRepository GroupPrincipalRepository { get; set; }
[Inject]
public DefaultController(
IGroupPrincipalRepository groupPrincipalRepository) {
this.GroupPrincipalRepository = groupPrincipalRepository;
}
}
And here's the NinjectWebCommon.cs RegisterServices
method:
kernel.Bind(typeof(IGroupPrincipalRepository)).ToConstructor(
c =>
new GroupPrincipalRepository(new PrincipalContext(ContextType.Domain, "?", "?", "?", "?"))).InSingletonScope();
Now, this is how my other projects that use Ninject (but are ASP.NET MVC 3 on .NET 4) work and as far as I know this is what's needed to make everything work. So, why am I suddenly getting No parameterless constructor defined for this object. exceptions?
UPDATE
Here's the full NinjectWebCommon.cs
file:
[assembly: WebActivator.PreApplicationStartMethod(typeof(App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(App_Start.NinjectWebCommon), "Stop")]
namespace App_Start {
using System;
using System.DirectoryServices.AccountManagement;
using System.Repositories.ActiveDirectory;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
public static class NinjectWebCommon {
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
public static void Start() {
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
public static void Stop() {
bootstrapper.ShutDown();
}
private static IKernel CreateKernel() {
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
private static void RegisterServices(
IKernel kernel) {
kernel.Bind(typeof(IGroupPrincipalRepository)).ToConstructor(
c =>
new GroupPrincipalRepository(new PrincipalContext(ContextType.Domain, "", "", "", ""))).InSingletonScope();
}
}
}
UPDATE - Please look at my answer for a link and explanation of the solution to this problem
NinjectWebCommon.cs
file is doing and if not then I guess the answer is "no where". Again, there's confusion on my end because my other projects work this way and I've double and triple checked to make sure I'm not lying... – UpkeepNinjectMvcHttpApplicationPlugin
in the Ninject.MVC package which takes care of theDependecyResolver
... so it should just work? How do you installed Ninject.MVC ? With Nuget? – RagerNinjectWebCommon.cs
file as the Git hub docs say. I just tried another test with a fresh MVC 4 project with only a singleActionResult
in a single controller with one injection. Still fails with the same issue. – Upkeep