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",
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?
c # asp.net-mvc inversion-of-control ninject
ebb Dec 05 '10 at 10:44 2010-12-05 10:44
source share