MVC3 + Ninject - How to?
You are mixing an own dependency resolver with the MVC extension. I'd suggest either going with your own dependency resolver or with using the MVC extension but not both. When using the MVC extension you have to use OnApplicationStarted instead of Application_Start.
See http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/ and have a look at the SampleApplication that comes with the source code of the MVC extension https://github.com/ninject/ninject.web.mvc.
Also the fix is not used anymore when you use the current version for the build server: http://teamcity.codebetter.com
UPDATE: The Ninject.MVC3
package continues to be updated and works OOTB against MVC4 RTM (and RC). See this page in the wiki for details.
ebb
Updated on June 01, 2022Comments
-
ebb almost 2 years
I've just started playing with IoC containers and therefore chosed Ninject.
After several hours of sweat and tears I still cant figure out how to setup my MVC3 application with Ninject.
So far I have:
Global.asax.cs
public class MvcApplication : Ninject.Web.Mvc.NinjectHttpApplication { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); } protected void Application_Start() { DependencyResolver.SetResolver(new MyDependencyResolver(CreateKernel())); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } protected override IKernel CreateKernel() { var modules = new [] { new ServiceModule() }; return new StandardKernel(modules); } } ServiceModule.cs internal class ServiceModule : NinjectModule { public override void Load() { Bind<IGreetingService>().To<GreetingService>(); } }
MyDependencyResolver.cs
public class MyDependencyResolver : IDependencyResolver { private IKernel kernel; public MyDependencyResolver(IKernel kernel) { this.kernel = kernel; } public object GetService(System.Type serviceType) { return kernel.TryGet(serviceType); } public System.Collections.Generic.IEnumerable<object> GetServices(System.Type serviceType) { return kernel.GetAll(serviceType); } }
GreetingService.cs
public interface IGreetingService { string Hello(); } public class GreetingService : IGreetingService { public string Hello() { return "Hello from GreetingService"; } }
TestController.cs
public class TestController : Controller { private readonly IGreetingService service; public TestController(IGreetingService service) { this.service = service; } public ActionResult Index() { return View("Index", service.Hello()); } }
Each time I try to load the Index view it either just throws a overflow exception or a HTTP 404 error - If I remove all the Ninject code it works perfectly, whats wrong?
-
btype almost 13 years-1. I went to your link to download your sample app. It's missing files and won't compile
-
Remo Gloor almost 13 years@Christian Payne All is there. Just read the how too! The CI Server ensures it is buildable
-
Nicolas Fall over 12 yearsyou have to use OnApplicationStarted and inherite from
NinjectHttpApplication
? where do I put my legacyvoid Global_PostReleaseRequestState(object sender, EventArgs e)
,public override void Init()
, andvoid Global_PreRequestHandlerExecute(object sender, EventArgs e)
? -
Admin over 11 yearsThe API has changed too much over time, though I can tell a lot of this might have been brought about by changes to the MVC framework. This is a fairly simple concept and yet I've been reading conflicting documentation and blogs for quite some time now.