Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
163 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...