Ninject with MVC3 RTM

2020-04-15 20:54发布

问题:

I've upgraded MVC3 from RC2 to RTM. We were using Ninject 2.1.0.76, but things stopped working once I upgraded. So I used the NuGet manager to get the latest Ninject, Ninject.MVC3 and Ninject.Web.Mvc libraries (2.1.0.91, 1.0.0.0 and 2.1.0.39 respectively). Now, it creates an AppStart_NinjectMVC3 file.

I removed NinjectHttpApplication from my global.asax and made it back into a regular HttpApplication. When I tried to build, I get;

"Exception has been thrown by the target of an invocation"

Looking further, if I disable the following line;

DependencyResolver.SetResolver(new NinjectServiceLocator(kernel));

The build goes through. But I'm pretty sure I don't want to do this.

Any ideas?

----- UPDATE ---------

I created a new MVC3 project, added the reference to Ninject.MVC3 and this builds and runs fine. I compared web.config, don't see any differences that relate to Ninject or MVC in the two. A fresh project doesn't add the Ninject.Web.Mvc library, so I removed it and commented out all code relating to that, and still, the error occurs.

回答1:

Since ASP.NET MVC 3 Beta the IServiceLocator interface is replaced by IDependencyResolver. I'm not sure Ninject.MVC3 already has a release where they have implemented this interface.

Judging from the line DependencyResolver.SetResolver(new NinjectServiceLocator(kernel)) it appears they have not.

Here's a simple implementation of this interface for Ninject.

UPDATE: The Ninject.Web.Mvc library has a NinjectDependencyResolver class that extends from the IDependencyResolver interface. I think you should use this one (I do and everything works fine).



回答2:

Download the dlls from here

https://github.com/ninject/ninject/archives/master

https://github.com/ninject/ninject.web.mvc/archives/master

Further more do not use Ninject.MVC3 all you need ist Ninject.Web.Mvc

I also have an article documenting same here