MVC3 + Ninject - How?

I just started playing with IoC containers and therefore blocked Ninject.

After hours of sweat and tears, I still can't figure out how to set up an MVC3 application using Ninject.

So far I:

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 simply throws an overflow exception or an HTTP 404 error. If I delete all the Ninject code, it works fine, what’s wrong?

+16
c # asp.net-mvc inversion-of-control ninject
Dec 05 '10 at 10:44
source share
1 answer

You mix your own dependency converter with the MVC extension. I suggest either going with your own dependency converter, or using the MVC extension, but not both. When using the MVC extension, you should use OnApplicationStarted instead of Application_Start.

See http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/ and look at SampleApplication, which comes with the MVC extension source code https: // github .com / ninject / ninject.web.mvc .

Also, the fix is ​​no longer used when using the current version for the build server: http://teamcity.codebetter.com




UPDATE: The Ninject.MVC3 package continues to be updated and OOTB works with RTM (and RC) MVC4. See this page on the wiki for more details.

+15
Dec 05 '10 at 15:52
source share



All Articles