MVC3 + Ninject - 如何? [英] MVC3 + Ninject - How to?

查看:135
本文介绍了MVC3 + Ninject - 如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始IoC容器中,玩,因此chosed Ninject。

I've just started playing with IoC containers and therefore chosed Ninject.

几个小时,汗水和泪水之后我还是无法弄清楚如何设置我的Ninject MVC3应用程序。

After several hours of sweat and tears I still cant figure out how to setup my MVC3 application with Ninject.

到目前为止,我有:

的Global.asax.cs

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

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

GreetingService.cs

public interface IGreetingService
{
    string Hello();
}

public class GreetingService : IGreetingService
{
    public string Hello()
    {
        return "Hello from GreetingService";
    }
}

TestController.cs

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());
    }

}

每次我尝试加载索引视图,要么只是抛出一个溢出异常或HTTP 404错误 - 如果我删除所有Ninject code将其完美的作品,什么是错

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?

推荐答案

您正与MVC扩展混合一个自己的依赖解析器。我建议要么用自己的依赖解析器或使用MVC扩展,但不能同时去。 当使用MVC扩展你必须使用OnApplicationStarted代替的Application_Start。

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.

请参阅http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/而看看自带的MVC扩展<一源$ C ​​$ c中的SampleApplication href=\"https://github.com/ninject/ninject.web.mvc\">https://github.com/ninject/ninject.web.mvc.

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.

另外,修复程序不再当您使用当前版本为构建服务器使用: HTTP://teamcity.$c$cbetter .COM

Also the fix is not used anymore when you use the current version for the build server: http://teamcity.codebetter.com

更新:在 Ninject.MVC3 包将被继续更新和作品开箱即用的对MVC4 RTM(和RC)。具体细节看维基 这个网页。

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.

这篇关于MVC3 + Ninject - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆