UPDATE

Starting a Bounty, I have added is a Visual Studio 2010 solution replicating the Ninject bug as this seems to be quite a localised issue.

Ninject VS 2010 Solution


When I do the following:

  1. Start Visual Studio 2010 (yes 2010 :-( )

  2. File > New Project > Empty MVC 4 Application

  3. Manage Packages > install package Ninject.Web.Common (which then installs Ninject and Ninject.Mvc)

  4. Then do the following in a HomeController

Code:

    public class HomeController : Controller
    {

        public HomeController(IKernel kernel)
        {
            // Should have been injected
        }
    }

I get this:

Server Error in '/' Application.

No parameterless constructor defined for this object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

Stack Trace: 

[MissingMethodException: No parameterless constructor defined for this object.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +113
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
   System.Activator.CreateInstance(Type type) +6
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +55

[InvalidOperationException: An error occurred when trying to create a controller of type 'NinjectTest.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +179
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +197
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +49
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

When I checked to see what the DependencyResolver was I saw this:

enter image description here

When I do the same with a VS 2013 Express instance and create a fresh MVC 4 empty web application I see that the DependencyResolver is the Ninject one.

I do have a runnable VS 2010 solution but all external file storage sites are blocked at my workplace so I will link it latter, if anyone has any thoughts in the meantime....

My solution in the meantime is to wire up the resolver manually but I shouldn't have to do this.

有帮助吗?

解决方案

You need the Ninject.Mvc3 package installed which is the one defining and setting the dependency resolver. I can see in the VS2010 solution you have uploaded that Ninject.Web.Mvc.dll is not referenced. Installing that package will also install Ninject and Ninject.Web.Common, but not the other way round.

Once you have installed and referenced that package (Ninject.Mvc3), you may get the same issue using it with MVC 4 as here in this other question. Make sure the web.config is not missing a redirect for the System.Web.Mvc assembly, from version 3.0.0.0 to 4.0.0.0. The following line will redirect any version of System.Web.Mvc to version 4.0.0.0:

<dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>

Hope it helps!

其他提示

You need to set the resolver in the CreateKernel method.

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

Create kernel method:

    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        RegisterServices(kernel);
        DependencyResolver.SetResolver(new NinjectServiceLocator(kernel));

        return kernel;
    }

To register your services:

    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<ITheService>().To<TheService>();
        //add other services, etc
    }

You can get the Ninject dependency resolver from this nuget.

Hope this helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top