ASP.NET MVC 2、Ninject 2.2 且没有为此对象定义无参数构造函数

发布于 2024-11-02 21:29:14 字数 2292 浏览 0 评论 0原文

因此,我花了一些时间使用 ASP.NET MVC 2(目前一直使用 Visual Studio 2008),现在开始使用 Ninject 2.2 及其 MVC 集成。我从以下位置下载了 Ninject 2.2 和 Ninject.Web.Mvc:

https://github.com/downloads/ninject/ninject/Ninject-2.2.0.0-release-net-3.5.zip
https:// /github.com/downloads/ninject/ninject.web.mvc/Ninject.Web.Mvc2-2.2.0.0-release-net-3.5.zip

并在我的 MVC 2 项目中引用了它们。我的 Global.asax.cs 文件如下所示(与 Ninject.Web.Mvc 自述文件中的内容大致相同):

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject.Web.Mvc;
using Ninject;

namespace Mvc2 {
    public class MvcApplication : NinjectHttpApplication {
        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 override void OnApplicationStarted() {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }

        protected override IKernel CreateKernel() {
            var kernel = new StandardKernel();

            kernel.Bind<IFoo>().To<Foo>();

            return kernel;
        }
    }
}

家庭控制器如下所示:

using System;
using System.Web;
using System.Web.Mvc;

namespace Mvc2.Controllers {
    public class HomeController : Controller {
        private readonly IFoo foo;

        public HomeController(IFoo foo) {
            this.foo = foo;
        }

        public ActionResult Index() {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }
    }
}

现在,每次我运行我的项目并访问“/”时,我都会看到黄色屏幕死亡并显示一条消息:“没有为此对象定义无参数构造函数。”看起来 Ninject 没有解析我的 Foo 服务并将其注入 HomeController 中。我想我错过了一些非常明显的东西,但我只是没有看到它。

如何让 Ninject 将 Foo 注入 HomeController,而不使用 Ninject 属性?

So I've been spending some time with ASP.NET MVC 2 (currently stuck with using Visual Studio 2008) and have now moved onto using Ninject 2.2 and its MVC integration. I've downloaded Ninject 2.2 and Ninject.Web.Mvc from the following locations:

https://github.com/downloads/ninject/ninject/Ninject-2.2.0.0-release-net-3.5.zip
https://github.com/downloads/ninject/ninject.web.mvc/Ninject.Web.Mvc2-2.2.0.0-release-net-3.5.zip

And referenced them in my MVC 2 project. My Global.asax.cs file looks like this (pretty much what the Ninject.Web.Mvc README says):

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject.Web.Mvc;
using Ninject;

namespace Mvc2 {
    public class MvcApplication : NinjectHttpApplication {
        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 override void OnApplicationStarted() {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }

        protected override IKernel CreateKernel() {
            var kernel = new StandardKernel();

            kernel.Bind<IFoo>().To<Foo>();

            return kernel;
        }
    }
}

And a home controller that looks like this:

using System;
using System.Web;
using System.Web.Mvc;

namespace Mvc2.Controllers {
    public class HomeController : Controller {
        private readonly IFoo foo;

        public HomeController(IFoo foo) {
            this.foo = foo;
        }

        public ActionResult Index() {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }
    }
}

Now every time I run my project and visit '/' I get a yellow screen of death with a message that says "No parameterless constructor defined for this object." It seems Ninject is not resolving my Foo service and injecting it into HomeController. I imagine I'm missing something really obvious but I'm just not seeing it.

How do I get Ninject to inject Foo into the HomeController, and without using Ninject attributes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Saygoodbye 2024-11-09 21:29:14

:您能否提供有关 IFoo 服务实现的更多信息?它自己的所有依赖项是否都得到满足?

我自己:嗯,不,不是。事实证明我没有绑定它的依赖项。天哪,错误消息和堆栈跟踪是否具有误导性!


所以我的错误是我没有绑定 IFoo 实现的依赖项之一,因此 Ninject 无声地失败并尝试继续其快乐的方式。这真的很不幸,因为一旦我偏离了一个微不足道的设置,它可能会导致一些非常奇怪的行为。我想我的下一个问题应该是如何让 Ninject 尽早失败并提供有关问题所在的良好消息?但现在我至少可以继续下去。

Me: Could you provide a little more information about the IFoo service implementation? Does it have all of its own dependencies satisfied?

Myself: Hmm, no it doesn't. Turns out I didn't bind its dependencies. Boy, is that error message and stack trace misleading!


So my mistake was that I didn't bind one of the dependencies of the IFoo implementation and so Ninject failed silently and tried to continue on its merry way. Which is really unfortunate because it could lead to some really strange behavior once I deviate from a trivial setup. I guess my next question should be how can I get Ninject to fail as early as possible and provide good messaging about what's wrong? But for now I can at least get on with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文