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
question from:
https://stackoverflow.com/questions/12311479/asp-net-mvc-4-ninject-mvc-3-no-parameterless-constructor-defined-for-this-ob 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…